导航

Remove Duplicates from Sorted List

Posted on 2014-11-08 02:20  八竿子打不着  阅读(115)  评论(0)    收藏  举报

这次是做题最顺的一次,提交两次,耗时1分钟就ac了,错的那次是因为没有考虑到空链表,少了一个判断。

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