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.
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:
-
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.
-
Initialize two pointers,
preandcur, to the dummy node and the head node respectively. -
Start a while loop that continues as long as
curandcur.nextare notNone. This is to ensure that we don't access aNonenode's value, which would raise an error. -
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.
-
If a duplicate is found, start another loop to skip all nodes with the same value. After this inner loop,
curwill either beNoneor a node with a different value. -
Set
pre.nexttocurto remove the duplicates from the list. -
If no duplicate is found, move
preandcurone step forward. -
After the loop, return
dummy.nextas the head of the modified list. The dummy node itself is not part of the original list, so we return its next node.
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.
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.