Leetcode No.82 delete duplicate Element II in the sorting linked list

Title Description

There is a linked list arranged in ascending order. Give you the head node of the linked list. Please delete all nodes with duplicate numbers in the linked list and only keep the numbers that do not appear repeatedly in the original linked list.

Returns a linked list of results in the same ascending order.

Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5]

Example 2: Input: head = [1,1,1,2,3] Output: [2,3]

Tips: The number of nodes in the linked list is within the range [0, 300] -100 <= Node.val <= 100 The title data ensures that the linked list has been arranged in ascending order

Problem solving idea I

Scan the linked list once, count the number of occurrences in the linked list, traverse the linked list again, and delete the node when the number of occurrences of the value of the node is greater than or equal to 2

import utils.ListNode;
import java.util.HashMap;
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode newHead=head;
        HashMap<Integer,Integer> map=new HashMap();
        while(head!=null){
            int value=head.val;
            if(map.get(value)==null){
                map.put(value,1);
            }else{
                map.put(value,map.get(value)+1);
            }
            head=head.next;
        }
        ListNode sentinel=new ListNode(0);
        sentinel.next=newHead;
        ListNode pre=sentinel,cur=newHead;
        while(cur!=null){
            if(map.get(cur.val)>=2){
                pre.next=cur.next;
                cur=cur.next;
            }else{
                pre=pre.next;
                cur=cur.next;
            }
        }
        return sentinel.next;
    }
    public static void main(String[] args) {
        ListNode head=new ListNode(1);
        ListNode node2=new ListNode(1);
        head.next=node2;
        ListNode node3=new ListNode(2);
        node2.next=node3;
        node3.next=null;
        Solution solution=new Solution();
        solution.deleteDuplicates(head);
    }
}

Complexity analysis

Time complexity: O(n), where n is the length of the linked list.

Spatial complexity: O(n), where n is the length of the linked list.

Problem solving idea 2

Because the given linked list is in good order, the positions of repeated elements in the linked list are continuous. Therefore, we only need to traverse the linked list once to delete the repeated elements. Since the head node of the linked list may be deleted, we need to use an additional sentinel node to point to the head node of the linked list.

Specifically, we point to the dummy node of the linked list from the pointer cur, and then start traversing the linked list. If the elements corresponding to cur.next and cur.next.next are the same, we need to delete cur.next and all linked list nodes with the same element value. We write down the element value x, and then continue to remove cur.next from the linked list until cur.next is an empty node or its element value is not equal to X. At this point, we will delete all nodes with element value x in the linked list.

If the elements corresponding to cur.next and cur.next.next are different, it means that there is only one node with an element value of cur.next in the linked list, so we can point cur to cur.next.

After traversing the entire linked list, we can return sentinel.next, the next node of the sentinel node of the linked list.

details

Note that cur.next and cur.next.next may be empty nodes. If they are not judged, running errors may occur.

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode sentinel=new ListNode(0,head);
        ListNode cur=sentinel;
        while(cur.next!=null&&cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int x=cur.next.val;
                while(cur.next!=null&&cur.next.val==x){
                    cur.next=cur.next.next;
                }
            }else{
                cur=cur.next;
            }
        }
        return sentinel.next;
    }

    public static void main(String[] args) {
        ListNode head=new ListNode(1);
        ListNode node2=new ListNode(1);
        head.next=node2;
        ListNode node3=new ListNode(2);
        node2.next=node3;
        node3.next=null;
        Solution solution=new Solution();
        solution.deleteDuplicates(head);
    }
}

Complexity analysis

Time complexity: O(n), where n is the length of the linked list.

Space complexity: O(1).

Posted on Mon, 29 Nov 2021 01:51:27 -0500 by explorer