Leetcode 203 Remove Linked List Elements

Photo by Billy Huynh on Unsplash

Leetcode 203 Remove Linked List Elements

A useful tool: sentinel node

·

3 min read

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

Example 1:

Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]

Example 2:

Input: head = [], val = 1
Output: []

Example 3:

Input: head = [7,7,7,7], val = 7
Output: []

Constraints:

  • The number of nodes in the list is in the range [0, 10<sup>4</sup>].

  • 1 <= Node.val <= 50

  • 0 <= val <= 50

Steps

  • Start by checking if the head of the linked list contains the target value

  • if the head node contains the target value, remove it and update the head pointer to point to the next node

  • iterate through the rest of the list, check each node for the target value and remove it if found

Solution

Issue

  • We need to handle the head node separately because there's no front node available

Sentinel Node or Dummy Node

In computer science, a sentinel node is like a special marker or signal used in data structures, such as linked lists or trees. See more

Sentinel nodes can be very useful in algorithms and data structures:

  1. Simplified Logic: Sentinel nodes can simplify the logic of algorithms by providing a consistent reference point. For example, in a linked list, instead of handling special cases for an empty list or the first/last node separately, you can treat all nodes uniformly.

  2. Efficiency: Sentinel nodes can improve the efficiency of algorithms by avoiding the need for additional checks or conditionals. For instance, they can eliminate the need to check for null pointers or empty data structures, reducing the number of conditional branches and improving performance.

  3. Boundary Handling: Sentinel nodes help in handling boundary cases more elegantly. They act as placeholders at the beginning or end of a data structure, making it easier to handle edge cases without special treatment.

  4. Simplified Insertion and Deletion: When working with linked data structures, sentinel nodes can simplify insertion and deletion operations by providing fixed reference points. This simplifies the code and reduces the risk of errors.

Solution with Sentinel Node

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
const removeElements = function(head, val) {
    let dummy = new ListNode(0, head);
    let curr = dummy;
    while (curr && curr.next) {
        if (curr.next.val === val) {
            curr.next = curr.next.next;
            continue;
        }
        curr = curr.next;
    }
    return dummy.next;
}

Conclusion

In summary, using a sentinel node in LeetCode problem 203 simplifies the algorithm's logic, improves efficiency, and enhances boundary case handling compared to the traditional approach without a sentinel node.