Java - delete duplicate nodes in the linked list

topic link

Niuke online oj question - delete duplicate nodes in the linked list

topic description

In a sorted linked list, there are duplicate nodes, please delete the duplicate nodes in the linked list, the duplicate nodes are not kept, and return the linked list head pointer. For example, linked list 1->2->3->3->4->4->5 is processed as 1->2->5

Data range: the length of the linked list satisfies 0≤n≤1000, and the value in the linked list satisfies 1≤val≤1000

Advanced: space complexity O(n), time complexity O(n)

For example, when {1,2,3,3,4,4,5} is input, the corresponding output is {1,2,5}, and the corresponding input and output linked list is shown in the figure below:
insert image description here

Topic example

Example 1

Input:
{1,2,3,3,4,4,5}

Return value:
{1,2,5}

Example 2

Input:
{1,1,1,8}

Return value:
{8}

problem solving ideas

Deleting a node in the linked list needs to determine the previous node of this node, so define a prev pointer to maintain the previous node of the deletion sequence

To determine the repeating sequence, you need to traverse the entire array and define a last node. When last.val and last.next.val are equal, last = last.next, and the prev node keeps its original position until last.val != last.next. val, at this time prev.next = last.next

And it should be noted that the first few nodes of the linked list may also be equal, so it is not easy to confirm the position of the head node. Since the values ​​in the linked list are all greater than or equal to 1, a sentinel head node head with a value of 0 can be defined, head. next = phead, it is convenient for us to delete the phead node, and finally return directly to head.next

For example:

First add the sentinel head node head, define prev and last pointers
insert image description here
At this time last.val
insert image description here
! Go backward
insert image description here
At this time last.val != last.next.val, make last walk backward alone
insert image description here
At this time last.val != last.next.val, make prev.next = last.next, last = last.next
insert image description here
at this time last.val != last.next.val, make last go backwards alone
insert image description here
At this time last.val != last.next.val, prev.next = last.next, last = last.next last.next
insert image description here
becomes null, last also traverses to null at the end, directly return to head.next to get the deleted linked list

full code

/*
 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){
    
    
            return null;
        }
        ListNode head = new ListNode(0);
        head.next = pHead;
        ListNode prev = head;
        ListNode last = head.next;
        while(last != null){
    
    
            //确定重复区域的起始位置
            while(last.next != null && last.val != last.next.val){
    
    
                prev = last;
                last = last.next;
            }
            //确定重复区域的终止位置
            while(last.next != null && last.val == last.next.val){
    
    
                last = last.next;
            }
            if(prev.next != last){
    
    
                prev.next = last.next;
            }
            last = last.next;
        }
        return head.next;
    }
}

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130228493