Linked list problem (Leetcode) – understanding inputs

Question:

I am starting to complete some Leetcode problems, having finished an online Data Structures and Algorithms course, but I’m struggling to understand what some of the input variables represent. I’ve included one of the solutions as an example. The problem gives me the head of a sorted linked list as input and asks me to remove all duplicates and return a new linked list as a solution.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None:
            return head
        pt=head
        prev=pt
        pt=pt.next
        while pt:
            if pt.val==prev.val:
# If the two nodes have the same value, then prev.next skips the current position of pt and moves two forward
                prev.next=pt.next
                pt=pt.next
            else:
                prev=pt
                pt=pt.next
        return head  

What I don’t understand is the meaning of the word ‘head’ here. For instance, if I have as an (incorrect) solution only one line of code that just returns head, the output will simply be equal to all of the inputs, implying that head is just the pointer that automatically iterates through all of the input values unless instructed to do otherwise. Yet I haven’t set up any iteration, while-loop etc, so why would the head variable move through the members of the input list automatically? Ordinarily, of course, the head is simply a pointer to the first member of the linked list. Then we set up code that allows us to iterate through the list while perhaps bringing a second pointer into play etc.

Frankly, for that matter (and here I’m going to reveal the extent of my confusion), I don’t really understand how the history of the pointers’ iteration through the linked list gives rise to a set of unique, non-duplicate values that can be returned as head in the solution presented. Prev is the only pointer that skips duplicate values, as I understand (‘if pt.val == prev.val// prev.next = pt.next…’), so how is it that by returning head here, we return the history of prev…? Any help would be much appreciated.

Asked By: Dommy1

||

Answers:

You can try this:

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        temp = head
        seen = set() # this will keep track if element is duplicate or not
        prev = head  # this will keep track of previous node
        while temp:
            if temp.val not in seen:
                seen.add(temp.val)
                prev = temp # update prev here only because if we update it in else as well then it can lead to point next of lost node you can get this by drawing this on paper
            else:
                prev.next = temp.next
            
            temp = temp.next
            
        return head