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
  1. Initialization :
    • Use a pointer
      current
      to traverse the linked list.
    • Start
      current
      at the head of the linked list.
  2. 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
      pointer of the current node to skip the next node, effectively removing the duplicate.
    • If the values are different, move the
      current
      pointer to the next node.
  3. End Traversal :
    • Continue this process until we reach the end of the linked list (
      current
      or
      current.next
      becomes
      null
      ).
    • The list is already sorted, so no need to perform additional sorting.
    Detailed Steps in Pseudocode
  4. Initialize Pointer :
    • Set
      current
      to point to the head of the linked list.
  5. While Loop :
    • Continue looping while
      current
      is not
      null
      and
      current.next
      is not
      null
      .
    • Inside the loop, check if
      current.val
      equals
      current.next.val
      .
  6. 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
      pointer to the next node.
  7. Return Result :
    • Once the loop terminates, the linked list will have all duplicates removed.

    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
    
                                            
    Step-by-Step Explanation of Pseudocode
  8. Definition of ListNode :
    • We define a
      ListNode
      class which contains an integer value
      val
      and a pointer
      next
      to the next node in the list.
  9. Function to delete duplicates :
    • The function
      deleteDuplicates
      accepts the
      head
      of the linked list as an argument.
    • It first checks if the head is
      null
      (i.e., the list is empty). If it is, it returns
      head
      immediately since there are no elements to process.
  10. Initialize
    current
    pointer
    :
    • Set the
      current
      pointer to the head of the linked list.
  11. While Loop :
    • We enter a while loop that runs as long as
      current
      and
      current.next
      are not
      null
      .
    • Inside the loop, we compare
      current.val
      with
      current.next.val
      .
  12. Remove Duplicate Node :
    • If
      current.val
      equals
      current.next.val
      , it indicates a duplicate. We set
      current.next
      to
      current.next.next
      to remove the duplicate.
    • If the values are not equal, we simply move
      current
      to
      current.next
      .
  13. Return Result :
    • Finally, after the while loop completes, we return the
      head
      of the modified linked list. The list will be free of duplicates and still in sorted order.
This detailed methodology and structured approach ensure that we understand how to remove duplicates from a sorted linked list effectively.