The Problem:
In this problem, we are given a sorted linked list and we have to remove all but one occurrence of duplicate nodes and return the new Linked List without breaking the sorted order.
Before we begin trying to figure out a solution for this problem, let’s first revisit an important concepts Linked List and Recursion.
Understanding Recursion:
Recursion is essentially a function calling itself. It is frequently used to solve problems that can be divided into smaller, similar sub-problems and can be very useful in dynamic programming. To understand recursion, it can be helpful to break it down into 3 parts:
- Base Condition: This condition prevents the recursive function from calling itself infinitely.
- Recursive Call: This is responsible for calling the recursive function itself.
- Small Calculation: This step, which can come before or after the Recursive Call, is responsible for performing some calculation that we need for the recursive function or for returning some result after a recursive call has been completed.
A recursive tree is a visual representation of the recursive calls made by a function. It can be very helpful in understanding how recursion works and in analyzing the time and space complexity of recursive algorithms.
Understanding Linked List:
A linked list is a linear data structure that consists of a sequence of nodes, each containing data and a reference to the next node in the list. It is often used as an alternative to arrays, as it can be more efficient in certain operations such as insertion and deletion.
To understand linked lists, it can be helpful to break it down into 3 parts:
- Nodes: Each node in a linked list contains some data and a reference to the next node in the list.
- Head: The head of a linked list is the first node in the list. It is used as the starting point for many operations on the list.
- Traversal: Traversing a linked list involves starting at the head and following the references from one node to the next until the desired node is reached or the end of the list is reached.
Linked lists can be very useful in solving problems that involve dynamic data structures, as they allow for efficient insertion and deletion of elements. They can also be used in combination with other data structures, such as stacks and queues, to solve more complex problems.
Intuition & Algorithm:
With an understanding of how recursion and linked lists work, let’s try to break down the problem. We don’t have to worry about making our own linked list data structure as it is given. This is a fairly straightforward problem, so we will begin by figuring out the base condition, which in this case would be when the recursion reaches the end of the linked list (head == None).
We can call the function recursively to get the next node. Once we reach the end of the Linked List, the recursive function will start to return the list nodes. We can then check if a given current node has a next node, and if it does, we can compare their values and replace the current node. We can reuse the same logic from Problem 237 to replace the nodes.
This ensures that we always have a sorted linked list with no duplicates. Let’s visualize the algorithm below.
Summary of Solution
Use recursion to remove duplicate nodes from a sorted linked list. Call the function recursively to get the next node and compare their values. If they are equal, replace the current node with its next node. Return the new head of the list.
Code
This can be translated to code in the following way:
Solution.py
lang: python
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution:def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:if head == None: # base casereturn headhead.next = self.deleteDuplicates(head.next) # recursive callif head.next and head.val == head.next.val: # small calculationhead.val == head.next.valhead.next = head.next.nextreturn headreturn head
- Time Complexity O(n)
- Space Complexity O(n)
The time complexity of this solution is O(n) This is because the function needs to traverse the linked lists to delete the duplicate nodes. Time taken for this operation would be proportional to the size of the linked list.
Space complexity is O(n) because of recursion call stack.
This approach is simple and efficient, making it a good solution to this problem.