Knowee
Questions
Features
Study Tools

Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list, and return the head of the modified linked list.

Question

Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list, and return the head of the modified linked list.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution using a dummy head and two pointers:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def deleteDuplicates(head):
    dummy = ListNode(0)
    dummy.next = head
    pre = dummy
    cur = head
    while cur and cur.next:
        if cur.val == cur.next.val:
            val = cur.val
            while cur and cur.val == val:
                cur = cur.next
            pre.next = cur
        else:
            pre = cur
            cur = cur.next
    return dummy.next

Here is a step-by-step explanation of the code:

  1. Create a dummy node and set its next node as the head of the linked list. This is to handle the edge case where the head node might be deleted.

  2. Initialize two pointers, pre and cur, to the dummy node and the head node respectively.

  3. Start a while loop that continues as long as cur and cur.next are not None. This is to ensure that we don't access a None node's value, which would raise an error.

  4. Inside the loop, check if the current node and the next node have the same value. If they do, then we have found a duplicate.

  5. If a duplicate is found, start another loop to skip all nodes with the same value. After this inner loop, cur will either be None or a node with a different value.

  6. Set pre.next to cur to remove the duplicates from the list.

  7. If no duplicate is found, move pre and cur one step forward.

  8. After the loop, return dummy.next as the head of the modified list. The dummy node itself is not part of the original list, so we return its next node.

This problem has been solved

Similar Questions

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1:Input: head = [1,1,2]Output: [1,2]Example 2:Input: head = [1,1,2,3,3]Output: [1,2,3] Constraints:The number of nodes in the list is in the range [0, 300].-100 <= Node.val <= 100The list is guaranteed to be sorted in ascending order.

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list.  You may return any such answer.

Write a function that searches a linked list for the integers that occur more than once, and removes all their occurrences except for the first occurrences. The function does not remove the integers that occur only once. Removed nodes must be freed. Each node is defined asstruct node{     int data;     struct node * next;};The function is declared as follows. Its input is the head pointer of the linked list. It returns the head pointer of the linked list after the removal.struct node *remove_repeated_integer(struct node *list);

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?Possible if X is not last node. Use following two steps (a) Copy the data of next of X to X. (b) Delete next of X.Possible if size of linked list is even.Possible if size of linked list is oddPossible if X is not first node. Use following two steps (a) Copy the data of next of X to X.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.