题目二:删除链表中重复的节点

 

    public ListNode deleteDuplication(ListNode pHead)
    {
        if(pHead==null){
            return null;
        }
        ListNode preNode = null;
        ListNode curNode = pHead;

        while(curNode != null){
            ListNode nextNode=curNode.next;
            boolean needDeleted = false;
            if(nextNode!=null && curNode.val == nextNode.val){
                needDeleted=true;
            }
            if(!needDeleted){
                preNode=curNode;
                curNode=curNode.next;
            }else {
                int value=curNode.val;
                ListNode toBeDeleted = curNode;

                while(toBeDeleted!=null && toBeDeleted.val == value){
                    nextNode=toBeDeleted.next;
                    toBeDeleted.next=null;
                    toBeDeleted=nextNode;
                }
                curNode=toBeDeleted;
                if(preNode==null){
                    pHead= curNode;
                }else{
                    preNode.next=curNode;
                }
            }
        }
        return pHead;
    }

 

posted @ 2018-01-21 16:35  Allen101  阅读(159)  评论(0编辑  收藏  举报