Remove Duplicates from Sorted List II

Keywords: Java C++ Algorithms DataStructure

Source

Given a sorted linked list, delete all nodes that have duplicate numbers,
leaving only distinct numbers from the original list.

Example
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Problem solution

The previous topic is to retain one of the duplicate value nodes. Deleting all duplicate nodes seems to make little difference. However, considering the uncertainty of the chain header (which may be deleted or retained), more if conditional statements are required in the traditional way. Here is a method to deal with the uncertainty of the chain header node -- introducing dummy node

ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *node = dummy;

Introduce A new pointer variable dummy and assign its next variable to head. Considering that the original chain header node may be deleted, it should be processed from dummy. Here, the head variable is reused. Consider the linked list A - > b - > C. when deleting B, you need to deal with and consider A and C, and point the next of A to C. In consideration of space efficiency, head can be used to replace the above node. The meaning is the same, and node is easier to understand.

Different from the previous question, because this question introduces a new node dummy, node - > Val = = node - > next - > Val can no longer be used for two reasons:

  1. This problem needs to delete all nodes with equal values, and the operation of deleting the linked list is related to the two nodes before and after the node, so it needs to involve three linked list nodes. When deleting a one-way linked list node, you cannot delete the current node, but only change the node pointed to by the next of the current node.
  2. When judging whether val is equal, first make sure that node - > next and node - > next - > next are not empty, otherwise they cannot be taken.

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == NULL) return NULL;

        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode *node = dummy;
        while (node->next != NULL && node->next->next != NULL) {
            if (node->next->val == node->next->next->val) {
                int val_prev = node->next->val;
                // remove ListNode node->next
                while (node->next != NULL && val_prev == node->next->val) {
                    ListNode *temp = node->next;
                    node->next = node->next->next;
                    delete temp;
                }
            } else {
                node = node->next;
            }
        }

        return dummy->next;
    }
};

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode node = dummy;
        while(node.next != null && node.next.next != null) {
            if (node.next.val == node.next.next.val) {
                int val_prev = node.next.val;
                while (node.next != null && node.next.val == val_prev) {
                    node.next = node.next.next;
                }
            } else {
                node = node.next;
            }
        }

        return dummy.next;
    }
}

Source code analysis

  1. First, consider the exception. When the head is NULL, NULL is returned
  2. new is a dummy variable. Dummy - > next points to the original chain header.
  3. Use the new variable node and set it as the dummy header node to traverse.
  4. When the current node and the next node val are the same, save the current value first to facilitate the judgment of while loop termination conditions and the deletion of nodes. Note that this code is also relatively concise.
  5. Finally, it returns dummy - > next, that is, the header node required by the topic.

Complexity analysis

Two pointers (node.next and node.next.next) traverse, and the time complexity is O(2n). A dummy and intermediate cache variable are used, and the space complexity is approximately O(1)

 

Posted by radman08 on Sun, 05 Dec 2021 07:01:31 -0800