|
| 1 | +--- |
| 2 | +id: remove-linked-list-elements |
| 3 | +title: Remove Linked List Elements |
| 4 | +sidebar_label: 203-Remove-Linked-List-Elements |
| 5 | +tags: |
| 6 | +- Linked List |
| 7 | +- Java |
| 8 | +- C++ |
| 9 | +- Python |
| 10 | +- Data Structures |
| 11 | +description: "This document provides solutions for removing all nodes from a linked list that have a specific value." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem |
| 15 | + |
| 16 | +Given the head of a linked list and an integer `val`, remove all the nodes of the linked list that have `Node.val == val`, and return the new head. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +**Input:** head = [1,2,6,3,4,5,6], val = 6 |
| 23 | + |
| 24 | +**Output:** [1,2,3,4,5] |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +**Input:** head = [], val = 1 |
| 29 | + |
| 30 | +**Output:** [] |
| 31 | + |
| 32 | +**Example 3:** |
| 33 | + |
| 34 | +**Input:** head = [7,7,7,7], val = 7 |
| 35 | + |
| 36 | +**Output:** [] |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- The number of nodes in the list is in the range `[0, 10^4]`. |
| 41 | +- `1 <= Node.val <= 50` |
| 42 | +- `0 <= val <= 50` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Approach |
| 47 | + |
| 48 | +The approach involves iterating through the linked list and removing nodes that have the specified value. This can be achieved using a dummy node to handle edge cases where the head itself needs to be removed. |
| 49 | + |
| 50 | +### Steps: |
| 51 | + |
| 52 | +1. **Dummy Node:** |
| 53 | + - Create a dummy node that points to the head of the list. This helps to easily handle the case where the head node itself needs to be removed. |
| 54 | + |
| 55 | +2. **Iterate and Remove:** |
| 56 | + - Use a pointer to iterate through the list, checking each node's value. |
| 57 | + - If a node's value equals `val`, adjust the pointers to bypass this node. |
| 58 | + |
| 59 | +3. **Return New Head:** |
| 60 | + - Return the next node of the dummy node as the new head of the list. |
| 61 | + |
| 62 | +## Solution for Remove Linked List Elements |
| 63 | + |
| 64 | +### Java Solution |
| 65 | + |
| 66 | +```java |
| 67 | +class ListNode { |
| 68 | + int val; |
| 69 | + ListNode next; |
| 70 | + ListNode() {} |
| 71 | + ListNode(int val) { this.val = val; } |
| 72 | + ListNode(int val, ListNode next) { this.val = val; this.next = next; } |
| 73 | +} |
| 74 | + |
| 75 | +class Solution { |
| 76 | + public ListNode removeElements(ListNode head, int val) { |
| 77 | + // Create a dummy node to handle edge cases |
| 78 | + ListNode dummy = new ListNode(0); |
| 79 | + dummy.next = head; |
| 80 | + |
| 81 | + // Use a pointer to iterate through the list |
| 82 | + ListNode current = dummy; |
| 83 | + |
| 84 | + while (current.next != null) { |
| 85 | + if (current.next.val == val) { |
| 86 | + // Bypass the node with the value equal to val |
| 87 | + current.next = current.next.next; |
| 88 | + } else { |
| 89 | + // Move to the next node |
| 90 | + current = current.next; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Return the new head |
| 95 | + return dummy.next; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | +### C++ solution |
| 100 | + |
| 101 | +```cpp |
| 102 | +struct ListNode { |
| 103 | + int val; |
| 104 | + ListNode *next; |
| 105 | + ListNode() : val(0), next(nullptr) {} |
| 106 | + ListNode(int x) : val(x), next(nullptr) {} |
| 107 | + ListNode(int x, ListNode *next) : val(x), next(next) {} |
| 108 | +}; |
| 109 | + |
| 110 | +class Solution { |
| 111 | +public: |
| 112 | + ListNode* removeElements(ListNode* head, int val) { |
| 113 | + // Create a dummy node to handle edge cases |
| 114 | + ListNode* dummy = new ListNode(0); |
| 115 | + dummy->next = head; |
| 116 | + |
| 117 | + // Use a pointer to iterate through the list |
| 118 | + ListNode* current = dummy; |
| 119 | + |
| 120 | + while (current->next != nullptr) { |
| 121 | + if (current->next->val == val) { |
| 122 | + // Bypass the node with the value equal to val |
| 123 | + current->next = current->next->next; |
| 124 | + } else { |
| 125 | + // Move to the next node |
| 126 | + current = current->next; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Return the new head |
| 131 | + return dummy->next; |
| 132 | + } |
| 133 | +}; |
| 134 | +``` |
| 135 | +### Python Solution |
| 136 | +
|
| 137 | +```python |
| 138 | +class ListNode: |
| 139 | + def __init__(self, val=0, next=None): |
| 140 | + self.val = val |
| 141 | + self.next = next |
| 142 | +
|
| 143 | +class Solution: |
| 144 | + def removeElements(self, head: ListNode, val: int) -> ListNode: |
| 145 | + # Create a dummy node to handle edge cases |
| 146 | + dummy = ListNode(0) |
| 147 | + dummy.next = head |
| 148 | + |
| 149 | + # Use a pointer to iterate through the list |
| 150 | + current = dummy |
| 151 | + |
| 152 | + while current.next is not None: |
| 153 | + if current.next.val == val: |
| 154 | + # Bypass the node with the value equal to val |
| 155 | + current.next = current.next.next |
| 156 | + else: |
| 157 | + # Move to the next node |
| 158 | + current = current.next |
| 159 | + |
| 160 | + # Return the new head |
| 161 | + return dummy.next |
| 162 | +``` |
| 163 | +### Complexity Analysis |
| 164 | +**Time Complexity:** O(n) |
| 165 | + |
| 166 | +>Reason: The algorithm involves a single pass through the linked list, resulting in a time complexity of $O(n)$, where $n$ is the number of nodes in the list. |
| 167 | + |
| 168 | +**Space Complexity:** O(1) |
| 169 | +>Reason: The space complexity is $O(1)$ because the algorithm uses a constant amount of extra space. |
| 170 | +
|
| 171 | +### References |
| 172 | +**LeetCode Problem:** Remove Linked List Elements |
| 173 | + |
| 174 | +**Solution Link:** Remove Linked List Elements Solution on LeetCode |
| 175 | + |
| 176 | + |
0 commit comments