Skip to content

Commit 0ce1cfd

Browse files
committed
feat: add remove-nth-node-from-end-of-list
1 parent 254a054 commit 0ce1cfd

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Problem 0019: Remove Nth Node From End Of List
2+
3+
## Link
4+
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
5+
6+
## Intuition
7+
Use two pointers to efficiently locate the node to remove in a single pass by maintaining a fixed gap.
8+
9+
## Approach
10+
Create a dummy node, advance the first pointer n+1 steps, then move both pointers until the first reaches the end; remove the target node.
11+
12+
## Complexity
13+
- Time complexity: O(n), where n is the length of the list (single traversal).
14+
- Space complexity: O(1), as only a few pointers are used.
15+
16+
## Notes
17+
Alternative: calculate length in two passes; handle edge cases like removing the head or single-node lists.
18+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import *
2+
3+
class ListNode:
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
class Solution:
9+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
10+
dummy = ListNode()
11+
12+
dummy.next = head
13+
p1 = dummy
14+
15+
for _ in range(n + 1):
16+
p1 = p1.next
17+
18+
p2 = dummy
19+
20+
while p1:
21+
p2 = p2.next
22+
p1 = p1.next
23+
24+
p2.next = p2.next.next
25+
26+
return dummy.next
27+
28+
if __name__ == "__main__":
29+
def build_list(arr):
30+
dummy = ListNode()
31+
curr = dummy
32+
for v in arr:
33+
curr.next = ListNode(v)
34+
curr = curr.next
35+
return dummy.next
36+
37+
def to_list(node):
38+
res = []
39+
while node:
40+
res.append(node.val)
41+
node = node.next
42+
return res
43+
44+
s = Solution()
45+
list1 = build_list([1,2,3,4,5])
46+
list2 = build_list([1])
47+
answer_one = s.removeNthFromEnd(list1, 2)
48+
answer_two = s.removeNthFromEnd(list2, 1)
49+
print(to_list(answer_one)) # Expected: [1,2,3,5]
50+
print(to_list(answer_two)) # Expected: []

problems/0707-design-linked-list/solution.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def __init__(self):
1616
self.size = 0
1717

1818
def get(self, index: int) -> int:
19-
# print(index, self.size)
2019
if not self.checkElementAtIndex(index):
2120
return -1
2221

@@ -87,7 +86,6 @@ def deleteAtIndex(self, index: int) -> None:
8786
self.size -= 1
8887

8988
# utils
90-
9189
def checkElementAtIndex(self, index):
9290
return 0 <= index < self.size
9391

problems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Number | Title | Folder |
44
| ------ | ----- | ------ |
5+
| 0019 | Remove Nth Node From End Of List | [0019-remove-nth-node-from-end-of-list](0019-remove-nth-node-from-end-of-list) |
56
| 0021 | Merge Two Sorted Lists | [0021-merge-two-sorted-lists](0021-merge-two-sorted-lists) |
67
| 0217 | Contains Duplicate | [0217-contains-duplicate](0217-contains-duplicate) |
78
| 0707 | Design Linked List | [0707-design-linked-list](0707-design-linked-list) |

0 commit comments

Comments
 (0)