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
current
-
Start
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
next
-
If the values are different, move the
current
- End Traversal :
-
Continue this process until we reach the end of the linked list (
current
current.next
null
- The list is already sorted, so no need to perform additional sorting.
- Initialize Pointer :
-
Set
current
- While Loop :
-
Continue looping while
current
null
current.next
null
-
Inside the loop, check if
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
current
- Return Result :
- Once the loop terminates, the linked list will have all duplicates removed.
- Definition of ListNode :
-
We define a
ListNode
val
next
- Function to delete duplicates :
-
The function
deleteDuplicates
head
-
It first checks if the head is
null
head
-
Initialize
current
-
Set the
current
- While Loop :
-
We enter a while loop that runs as long as
current
current.next
null
-
Inside the loop, we compare
current.val
current.next.val
- Remove Duplicate Node :
-
If
current.val
current.next.val
current.next
current.next.next
-
If the values are not equal, we simply move
current
current.next
- Return Result :
-
Finally, after the while loop completes, we return the
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