Cattle off network - delete duplicate node list (list)

topic:

In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5

Knowledge points:

List

Ideas:

1. First add a head node, to prevent against the first, the second node on the same circumstances

2. Set pre, current pointer, pre pointer to the current node is determined not to repeat that, while the current pointer is equivalent to the work pointer, to have been behind the search.

answer:

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
        if(pHead==null || pHead.next==null){return pHead;}
        ListNode Head = new ListNode(0);//创建的头指针
        Head.next = pHead;
        ListNode pre = Head;
        ListNode current = pHead;
        while(current != null){
            if(current.next!=null && current.val == current.next.val){
                while(current.next!=null && current.val == current.next.val){
                    current = current.next;
                }
                pre.next = current.next;
                current = current.next;
            }else{
                pre = pre.next;
                current = current.next;
            }
        }
        return Head.next;
    }
}

 

Published 58 original articles · won praise 4 · Views 2936

Guess you like

Origin blog.csdn.net/qq_41853047/article/details/104554393