Remove Duplicates From Sorted List
To solve this coding challenge, we need to focus on modifying a sorted linked list to remove any duplicate elements. Let's break down the problem and build the solution step-by-step, ensuring all duplicates are removed and only unique elements remain. Here's a detailed explanation followed by pseudocode.
Explanation
Problem Breakdown
- We start with a sorted linked list, which means all the elements in the linked list are in ascending order.
- The goal is to traverse the linked list and remove any duplicate nodes such that each element appears only once in the resulting linked list.
- We return the modified linked list that retains the sorted order but has no duplicate elements.
Approach
- Initialization :
-
Use a pointer
to traverse the linked list.
current -
Start
at the head of the linked list.
current - Traverse the Linked List :
- While traversing, compare the value of the current node with the value of the next node.
-
If both values are the same, this indicates a duplicate. We need to change the
pointer of the current node to skip the next node, effectively removing the duplicate.
next -
If the values are different, move the
pointer to the next node.
current - End Traversal :
-
Continue this process until we reach the end of the linked list (
or
currentbecomescurrent.next).null - The list is already sorted, so no need to perform additional sorting.
- Initialize Pointer :
-
Set
to point to the head of the linked list.
current - While Loop :
-
Continue looping while
is not
currentandnullis notcurrent.next.null -
Inside the loop, check if
equals
current.val.current.next.val - Remove Duplicate Node :
- If the values are equal, adjust the pointers to skip over the duplicate node.
-
If the values are different, move the
pointer to the next node.
current - Return Result :
- Once the loop terminates, the linked list will have all duplicates removed.
- Definition of ListNode :
-
We define a
class which contains an integer value
ListNodeand a pointervalto the next node in the list.next - Function to delete duplicates :
-
The function
accepts the
deleteDuplicatesof the linked list as an argument.head -
It first checks if the head is
(i.e., the list is empty). If it is, it returns
nullimmediately since there are no elements to process.head -
Initialize
pointer :
current -
Set the
pointer to the head of the linked list.
current - While Loop :
-
We enter a while loop that runs as long as
and
currentare notcurrent.next.null -
Inside the loop, we compare
with
current.val.current.next.val - Remove Duplicate Node :
-
If
equals
current.val, it indicates a duplicate. We setcurrent.next.valtocurrent.nextto remove the duplicate.current.next.next -
If the values are not equal, we simply move
to
current.current.next - Return Result :
-
Finally, after the while loop completes, we return the
of the modified linked list. The list will be free of duplicates and still in sorted order.
head
Detailed Steps in Pseudocode
Pseudocode
// Definition of ListNode
class ListNode:
// Constructor to create a new node
def __init__(self, value):
self.val = value // Value of the node
self.next = None // Pointer to the next node
// Function to delete duplicates
function deleteDuplicates(head):
if head is None:
// If the list is empty, simply return the head
return head
// Initialize the current pointer to the head of the list
current = head
// Traverse the list until current and current.next are not null
while current is not null and current.next is not null:
// Check if current node value is equal to the next node value
if current.val == current.next.val:
// If they are equal, we have found a duplicate
// Skip the next node by linking current node to the node after the next node
current.next = current.next.next
else:
// If they are not equal, move the current pointer to the next node
current = current.next
// After removing duplicates, return the head of the list
return head