• leetcode 82 Remove Duplicates from Sorted List II ----- java


    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

    For example,
    Given 1->2->3->3->4->4->5, return 1->2->5.
    Given 1->1->1->2->3, return 2->3.

    去掉链表中出现过的重复的数字,然后返回链表。

    就直接遍历一次就好了,做好标记。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            
            if( head == null || head.next == null)
                return head;
            ListNode result = head;
            ListNode first = head;
            ListNode flag ;
            int DD = 0;
            if( head.val == head.next.val )
                DD = 1;
            int del = 0;
            while( first.next != null){
                flag = first.next;
                if( first.val == flag.val){
                    del = 1;
                }else{
                    if( del == 0 && !head.equals(first) ){
                        result.next = first;
                        result = result.next;
                    }
                    del = 0;
                }
                first = first.next;
            }
            if( del == 0){
                result.next = first;
                result = result.next;
            }
            result.next = null;
            if( DD == 1 )
                return head.next;
            else
                return head;
    
        }
    }
  • 相关阅读:
    createjs 利用createjs 写拼图功能
    canvas 利用canvas中的globalCompositeOperation 绘制刮奖 效果
    git 命令
    cmd 打开gitshell
    Grunt完成对LESS实时编译
    nodejs gulp less编辑
    canvas 绘制 矩形 圆形
    canvas绘制时钟
    juqery easyui
    vue-router(第2节课,妙味课堂)
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/5974387.html
Copyright © 2020-2023  润新知