Remove Duplicates from Sorted List

For example,
Given 1->1->2 , return 1->2 .

Given 1->1->2->3->3, return 1->2->3 java


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null){
            return head;
        }
        
        ListNode pre = head;
       
        
        while(pre != null && pre.next != null){
            if(pre.val == pre.next.val){
                pre.next = pre.next.next;
            }else{
                pre = pre.next;
            }
          
        }
        
        return head;
    }
}
相關文章
相關標籤/搜索