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.
current that starts at the head of the linked list.current pointer.current.val with current.next.val.current.val == current.next.val), skip the next node by setting current.next to current.next.next.current to the next node.Here’s the implementation in Python:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def deleteDuplicates(head: ListNode) -> ListNode:
current = head
while current and current.next:
if current.val == current.next.val:
# Skip the next node, it's a duplicate
current.next = current.next.next
else:
# Move to the next node
current = current.next
return head
current begins at the head of the linked list.while loop continues as long as current and current.next are not None.
current node is equal to the value of the next node (current.next.val), then we skip the next node by adjusting the next pointer of the current node (current.next = current.next.next).current pointer to the next node (current = current.next).n.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?