My 1ms Java solution


  • 0

    Certainly, there are other better solutions here. I just wanna share with u guys a possibly different idea. Well, hope everyone could receive their desired offer!!!

    /**
     * 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){
                return null;
           }
           ListNode cur = head;
           ListNode cur2 = head;
           ListNode tmp = cur;
           while(cur2!=null){
               if(cur2.val!=cur.val){
                   cur.next = cur2;
                   cur = cur2;
                   continue;
               }
               if(cur.next!=null&&cur.next.val!=cur.val){
                   cur = cur.next;
               }
               cur2 = cur2.next;
           }
           cur.next = cur2;
           return tmp;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.