(83)-(Remove Duplicates from Sorted List )-(从有序的单链表中删除重复值的节点)-(我只有一个很奇怪的疑问,为嘛用break就不能通过,改成return 就可以通过呢)

public class Solution 
{
    public ListNode deleteDuplicates(ListNode head) 
    {
         if(head==null||head.next==null)
        {
            return head;     
        }
        
        ListNode temp_head=head;
        //遍历到倒数第二个节点,因为删除是操作.next的
        while(temp_head.next!=null)
        {
            int curr_val=temp_head.val;
            int next_val=temp_head.next.val;
            while(curr_val==next_val)
            {
                //2,2,2,null ->2,2,null
                temp_head.next=temp_head.next.next;
                //在对下个元素的值进行更新前,现要判断他是不是空的说
                if(temp_head.next==null)
                {
                    return head;
                  //  break; 此处用break为啥就不通过呢
                }
                //对下个元素的值进行更新
                next_val=temp_head.next.val;     
            }
            temp_head=temp_head.next;
        }
        return head;      
    }
}

 

posted @ 2015-07-26 21:35  爱吃萝卜干  阅读(68)  评论(0编辑  收藏  举报