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. Examples:Input: Linked List = 23->28->28->35->49->49->53->53Output: 23 35Explanation: The duplicate numbers are 28, 49 and 53 which are removed from the list.Input: Linked List = 11->11->11->11->75->75Output: Empty listExplanation: All the nodes in the linked list have duplicates. Hence the resultant list would be empty.

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. Examples:Input: Linked List = 23->28->28->35->49->49->53->53Output: 23 35Explanation: The duplicate numbers are 28, 49 and 53 which are removed from the list.Input: Linked List = 11->11->11->11->75->75Output: Empty listExplanation: All the nodes in the linked list have duplicates. Hence the resultant list would be empty.

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

Solution

Here is a step-by-step solution in Python:

  1. First, we need to handle the edge case where the linked list is empty. If the head of the linked list is None, we simply return None.
if not head:
    return None
  1. We create a dummy node and set its next pointer to the head of the linked list. We also create two pointers, prev and curr, and set them to the dummy node and the head of the linked list, respectively.
dummy = ListNode(0)
dummy.next = head
prev = dummy
curr = head
  1. We start a while loop that continues until curr is not None. Inside the loop, we start another while loop that continues as long as the next node of curr is not None and its value is the same as the value of curr. If this is the case, we move curr to its next node.
while curr:
    while curr.next and curr.next.val == curr.val:
        curr = curr.next
  1. After the inner loop, if the next node of prev is not the same as curr, it means we have encountered duplicates. So, we skip all duplicates and link prev to the node after all duplicates. If not, we simply move prev to its next node.
    if prev.next != curr:
        prev.next = curr.next
    else:
        prev = prev.next
  1. Finally, we move curr to its next node and continue the outer loop.
    curr = curr.next
  1. After the outer loop, we return the next node of dummy, which is the head of the modified linked list.
return dummy.next

Here is the complete Python function:

def deleteDuplicates(head):
    if not head:
        return None

    dummy = ListNode(0)
    dummy.next = head
    prev = dummy
    curr = head

    while curr:
        while curr.next and curr.next.val == curr.val:
            curr = curr.next

        if prev.next != curr:
            prev.next = curr.next
        else:
            prev = prev.next

        curr = curr.next

    return dummy.next

This function takes a linked list and removes all nodes that have duplicate numbers, leaving only numbers that appear once in the original list. It returns the head of the modified linked list.

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.

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 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.

Given a doubly Linked list and a position. The task is to delete a node from a given position (position starts from 1) in a doubly linked list and return the head of the doubly Linked list.

2487. Remove Nodes From Linked ListSolvedMediumTopicsCompaniesHintYou are given the head of a linked list.Remove every node which has a node with a greater value anywhere to the right side of it.Return the head of the modified linked list. Example 1:Input: head = [5,2,13,3,8]Output: [13,8]Explanation: The nodes that should be removed are 5, 2 and 3.- Node 13 is to the right of node 5.- Node 13 is to the right of node 2.- Node 8 is to the right of node 3.Example 2:Input: head = [1,1,1,1]Output: [1,1,1,1]Explanation: Every node has value 1, so no nodes are removed. Constraints:The number of the nodes in the given list is in the range [1, 105].1 <= Node.val <= 105

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.