Clean 2 pointer


  • 0
    D

    /**

    • 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 curr = head;
        
        ListNode temp = curr.next;
        
        while(temp!=null)
        {
            
            if(temp.val!=curr.val)
            {
                curr.next=temp;
                curr=temp;
                
            }
            
            temp=temp.next;
        }
        curr.next=temp;
       return head;
    }
    

    }


Log in to reply
 

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