|
| 1 | +--- |
| 2 | +id: remove-nth-node-from-end-of-list |
| 3 | +title: Remove nth node from end of list (LeetCode) |
| 4 | +sidebar_label: 0019-remove-nth-node-from-end-of-list |
| 5 | +tags: |
| 6 | + - Two Pointers |
| 7 | + - Linked List |
| 8 | +description: "Given the head of a linked list, remove the nth node from the end of the list and return its head." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :-------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------ | |
| 14 | +| [Remove nth node from end of list](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) | [Remove nth node from end of list on LeetCode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/solutions/) | [Areetra Halder](https://leetcode.com/u/areetrahalder/) | |
| 15 | +### Problem Description |
| 16 | + |
| 17 | +Given the head of a linked list, remove the nth node from the end of the list and return its head. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +#### Example 1 |
| 22 | + |
| 23 | +- **Input:** `head = [1,2,3,4,5], n = 2` |
| 24 | +- **Output:** `[1,2,3,5]` |
| 25 | + |
| 26 | +#### Example 2 |
| 27 | + |
| 28 | +- **Input:** `head = [1], n = 1` |
| 29 | +- **Output:** `[]` |
| 30 | + |
| 31 | +#### Example 3 |
| 32 | + |
| 33 | +- **Input:** `head = [1,2], n = 1` |
| 34 | +- **Output:** `[1]` |
| 35 | + |
| 36 | +### Constraints |
| 37 | + |
| 38 | +- The number of nodes in the list is sz. |
| 39 | +- $1 <= sz <= 30$ |
| 40 | +- $0 <= Node.val <= 100$ |
| 41 | +- $1 <= n <= sz$ |
| 42 | +### Approach |
| 43 | +A challenging point of this question is that Linked List doesn't have index number, so we don't know which node is the last Nth node from the last. |
| 44 | + |
| 45 | +My strategy is to create dummy pointer and create distance dummy pointer and head pointer. |
| 46 | +### Solution Code |
| 47 | + |
| 48 | +#### Python |
| 49 | + |
| 50 | +```python |
| 51 | +class Solution: |
| 52 | + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: |
| 53 | + res = ListNode(0, head) |
| 54 | + dummy = res |
| 55 | + |
| 56 | + for _ in range(n): |
| 57 | + head = head.next |
| 58 | + |
| 59 | + while head: |
| 60 | + head = head.next |
| 61 | + dummy = dummy.next |
| 62 | + |
| 63 | + dummy.next = dummy.next.next |
| 64 | + |
| 65 | + return res.next |
| 66 | +``` |
| 67 | +#### Java |
| 68 | + |
| 69 | +```java |
| 70 | +class Solution { |
| 71 | + public ListNode removeNthFromEnd(ListNode head, int n) { |
| 72 | + ListNode res = new ListNode(0, head); |
| 73 | + ListNode dummy = res; |
| 74 | + |
| 75 | + for (int i = 0; i < n; i++) { |
| 76 | + head = head.next; |
| 77 | + } |
| 78 | + |
| 79 | + while (head != null) { |
| 80 | + head = head.next; |
| 81 | + dummy = dummy.next; |
| 82 | + } |
| 83 | + |
| 84 | + dummy.next = dummy.next.next; |
| 85 | + |
| 86 | + return res.next; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | +#### C++ |
| 91 | + |
| 92 | +```cpp |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + ListNode* removeNthFromEnd(ListNode* head, int n) { |
| 96 | + ListNode* res = new ListNode(0, head); |
| 97 | + ListNode* dummy = res; |
| 98 | + |
| 99 | + for (int i = 0; i < n; i++) { |
| 100 | + head = head->next; |
| 101 | + } |
| 102 | + |
| 103 | + while (head != nullptr) { |
| 104 | + head = head->next; |
| 105 | + dummy = dummy->next; |
| 106 | + } |
| 107 | + |
| 108 | + dummy->next = dummy->next->next; |
| 109 | + |
| 110 | + return res->next; |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
| 114 | +### Javascript |
| 115 | +```javascript |
| 116 | +var removeNthFromEnd = function(head, n) { |
| 117 | + let res = new ListNode(0, head); |
| 118 | + let dummy = res; |
| 119 | + |
| 120 | + for (let i = 0; i < n; i++) { |
| 121 | + head = head.next; |
| 122 | + } |
| 123 | + |
| 124 | + while (head) { |
| 125 | + head = head.next; |
| 126 | + dummy = dummy.next; |
| 127 | + } |
| 128 | + |
| 129 | + dummy.next = dummy.next.next; |
| 130 | + |
| 131 | + return res.next; |
| 132 | +}; |
| 133 | +``` |
| 134 | +## Step by Step Algorithm |
| 135 | +1: Initialize variables: |
| 136 | + |
| 137 | + - We create a dummy node res with a value of 0 and set its next pointer to the head of the original list. This dummy node helps in handling edge cases when removing the first node. |
| 138 | + - We initialize another pointer dummy to the dummy node res. This pointer will be used to traverse the list. |
| 139 | +``` |
| 140 | +res = ListNode(0, head) |
| 141 | +dummy = res |
| 142 | +``` |
| 143 | +2: Move head pointer forward by n nodes: |
| 144 | + |
| 145 | + - We iterate n times using a for loop to advance the head pointer n nodes forward. This effectively moves head to the nth node from the beginning. |
| 146 | +``` |
| 147 | +for _ in range(n): |
| 148 | + head = head.next |
| 149 | +``` |
| 150 | +3: Find the node before the node to be removed: |
| 151 | + |
| 152 | + - We use a while loop to traverse the list with both head and dummy pointers. |
| 153 | + - As long as head is not None, we move both head and dummy pointers one node forward in each iteration. |
| 154 | + - After this loop, dummy will be pointing to the node right before the node to be removed. |
| 155 | +``` |
| 156 | +while head: |
| 157 | + head = head.next |
| 158 | + dummy = dummy.next |
| 159 | +``` |
| 160 | +4: Remove the nth node from the end: |
| 161 | + |
| 162 | + - Once the loop finishes, dummy will be pointing to the node right before the node to be removed. |
| 163 | + - We update the next pointer of the node pointed by dummy to skip the next node, effectively removing the nth node from the end. |
| 164 | +``` |
| 165 | +dummy.next = dummy.next.next |
| 166 | +``` |
| 167 | +5: Return the modified list: |
| 168 | + |
| 169 | + - Finally, we return the next node after the dummy node res, which is the head of the modified list. |
| 170 | +``` |
| 171 | +return res.next |
| 172 | +``` |
| 173 | +This algorithm effectively removes the nth node from the end of the linked list by traversing it only once. |
0 commit comments