HomeLeetcode83. Remove Duplicates from Sorted List - Leetcode Solutions

83. Remove Duplicates from Sorted List – Leetcode Solutions

Description:

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Examples:

Example 1:
Input: head = [1,1,2]
Output: [1,2]

Example 2:
Input: head = [1,1,2,3,3]
Output: [1,2,3]

Solution in Python:

Python
# 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]:
        # Initialize current node as the head of the list
        current = head
        
        # Traverse the linked list until the end
        while current and current.next:
            # If the current node's value is the same as the next node's value
            if current.val == current.next.val:
                # Skip the next node by pointing the current node's next to the next node's next
                current.next = current.next.next
            else:
                # Move to the next node
                current = current.next
        
        # Return the modified list's head
        return head

Explanation:

  1. ListNode Class:
    • This is the definition of a singly-linked list node with a value (val) and a pointer to the next node (next).
  2. Current Pointer:
    • current: A pointer to traverse the linked list starting from the head.
  3. Traversal and Duplicate Removal:
    • We traverse the list with the current pointer until we reach the end of the list (while current and current.next).
    • For each node, we check if its value is the same as the value of the next node (current.val == current.next.val).
    • If they are the same, we skip the next node by setting the current.next pointer to current.next.next, effectively removing the duplicate node from the list.
    • If they are not the same, we simply move the current pointer to the next node.
  4. Return Result:
    • Finally, we return the head of the modified list, which now contains only unique elements in sorted order.

Solution in Javascript:

JavaScript
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    // Initialize current node as the head of the list
    let current = head;
    
    // Traverse the linked list until the end
    while (current !== null && current.next !== null) {
        // If the current node's value is the same as the next node's value
        if (current.val === current.next.val) {
            // Skip the next node by pointing the current node's next to the next node's next
            current.next = current.next.next;
        } else {
            // Move to the next node
            current = current.next;
        }
    }
    
    // Return the modified list's head
    return head;
};

Solution in Java:

Java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // Initialize the current node as the head of the list
        ListNode current = head;
        
        // Traverse the linked list until the end
        while (current != null && current.next != null) {
            // If the current node's value is the same as the next node's value
            if (current.val == current.next.val) {
                // Skip the next node by pointing the current node's next to the next node's next
                current.next = current.next.next;
            } else {
                // Move to the next node
                current = current.next;
            }
        }
        
        // Return the modified list's head
        return head;
    }
}

Solution in C#:

C#
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode DeleteDuplicates(ListNode head) {
        // Initialize the current node as the head of the list
        ListNode current = head;
        
        // Traverse the linked list until the end
        while (current != null && current.next != null) {
            // If the current node's value is the same as the next node's value
            if (current.val == current.next.val) {
                // Skip the next node by pointing the current node's next to the next node's next
                current.next = current.next.next;
            } else {
                // Move to the next node
                current = current.next;
            }
        }
        
        // Return the modified list's head
        return head;
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular