2021-06-21 repeating elements in ordered linked list 2

Posted by DavidT on Fri, 28 Jan 2022 00:39:54 +0100

leetcode daily question of deleting duplicate elements in the sorting linked list

Title Link: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/submissions/

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]

Solution 1:

Here we refer to a solution of the boss:

Considering some boundary conditions, such as 1 - > 1 - > 1 - > 2, the first ones need to be removed. We add a dummy node to facilitate boundary processing.

The initial two pointers are as follows:

  • Point the a pointer to the dummy node
  • Point the b pointer to head (the next node of the dummy node)

If the value pointed to by a is not equal to the value pointed to by b, both pointers advance one bit
Otherwise, move b alone, and b keeps moving forward until the value pointed by a is not equal to the value pointed by b.

Note that a.val==b.val is not directly compared here. This comparison is wrong, because at the beginning, a points to the dummy node, so the comparison logic should be as follows:

a.next.val == b.next.val

When the values pointed to by the two pointers are equal, b moves forward continuously. Here, it is judged by a while loop, because the repeated 2 of 1 - > 2 - > 2 - > 2 - > 3 should be filtered out.
The following is the process of the program

code:

//Input: head = [1,2,3,3,4,4,5]
    //Output: [1,2,5]
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        //Define a node to refer to the head node, because the head node may also be deleted
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        //A node points to a node
        ListNode a = dummy;
        //b node refers to the head node
        ListNode b = head;
        while (b != null && b.next != null) {
            //During initialization, a points to a node, so the comparison logic should be the value of the next node of a and the value of the next node of b
            if (a.next.val != b.next.val) {
                a = a.next;
                b = b.next;
            } else {
                //If the node values pointed to by a and b are equal, move b continuously until the values pointed to by a and b are equal
                while (b != null && b.next != null && a.next.val == b.next.val) {
                    //If the values are equal, b moves backward to see if they are still equal
                    b = b.next;
                }
                a.next = b.next;
                b = b.next;
            }
        }
        return dummy.next;
    }

For problems such as linked lists, you can draw the process of walking by yourself to prevent being dizzy

Solution 2:

The specific methods are as follows:

  1. Traverse the linked list and put the value of each node into the hash table. The key of the hash table is the value of the node, and value is the frequency of this value
  2. Traverse the hash table and put all key s with frequency = = 1 into the set
  3. Sort collection
  4. Traverse the collection, and then constantly create new linked list nodes

Of course, it can be optimized here. For example, using data structures such as LinkedHashMap (LinkedHashMap can record the order of putting elements and ensure that the order remains unchanged when taking them out) or OrderedDict can eliminate the sorting link.

Order of LinkedHashMap: https://blog.csdn.net/topc2000/article/details/79798513

Code implementation:

//Solution 2
    public ListNode deleteDuplicates2(ListNode head) {
        if(head==null || head.next==null) {
            return head;
        }
        //Record the occurrence frequency of each node value with a hash table
        HashMap<Integer,Integer> cache = new HashMap<Integer,Integer>();
        ArrayList<Integer> arr = new ArrayList<Integer>();
        ListNode p = head;
        while(p!=null) {
            int val = p.val;
            if(cache.containsKey(val)) {
                cache.put(val,cache.get(val)+1);
            } else {
                cache.put(val,1);
            }
            p = p.next;
        }
        //Put all values that only appear once into the ARR, and then sort the arr
        for(Integer k : cache.keySet()) {
            if(cache.get(k)==1) {
                arr.add(k);
            }
        }
        Collections.sort(arr);
        ListNode dummy = new ListNode(-1);
        p = dummy;
        //Create a linked list with the length of arr.length, and assign the values in arr to each linked list node in turn
        for(Integer i : arr) {
            ListNode tmp = new ListNode(i);
            p.next = tmp;
            p = p.next;
        }
        return dummy.next;
    }

**Reference link:** https://mp.weixin.qq.com/s/UJdtPllv5erPIZvMDQAuIg

Note: you can also directly search WeChat official account algorithm, basically with a map, and write very well, I will not generally refer to this big guy.

Topics: Java Algorithm data structure leetcode linked list