Leetcode83. Remove Duplicates from Sorted List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null) return null;
        
        ListNode pre = head,p = head.next;
        while(p!=null){
            if(p.val==pre.val){
                pre.next = p.next;
            }else{
                pre = pre.next;
            }
            p = pre.next;
        }
        return head;
    }
}

Runtime: 1 ms, faster than 21.21% of Java online submissions for Remove Duplicates from Sorted List.
Memory Usage: 37.8 MB, less than 21.98% of Java online submissions for Remove Duplicates from Sorted List.

posted @ 2019-03-28 00:19  大胖子球花  阅读(69)  评论(0)    收藏  举报