码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Duplicates from Sorted List

时间:2014-11-08 07:04:13      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   div   on   log   

这次是做题最顺的一次,提交两次,耗时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;
        }
    }
}

 

Remove Duplicates from Sorted List

标签:style   blog   io   color   sp   for   div   on   log   

原文地址:http://www.cnblogs.com/bluedreamviwer/p/4082656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!