Skip to content

Commit b1a73ef

Browse files
committed
adding algo
1 parent aceb78e commit b1a73ef

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
# Definition for singly-linked list.
5+
class ListNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
class Solution:
11+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
12+
# Create a dummy node to handle edge cases (e.g., removing the head)
13+
dummy = ListNode(0)
14+
dummy.next = head
15+
16+
# Initialize two pointers
17+
fast = dummy
18+
slow = dummy
19+
20+
# Move fast pointer n+1 steps ahead
21+
for _ in range(n + 1):
22+
fast = fast.next
23+
24+
# Move both pointers until fast reaches the end
25+
while fast:
26+
fast = fast.next
27+
slow = slow.next
28+
29+
# Skip the nth node from the end
30+
slow.next = slow.next.next
31+
32+
return dummy.next
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ListNode } from './ListNode';
2+
3+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
4+
// Create a dummy node to handle edge cases (e.g., removing the head)
5+
const dummy = new ListNode(0);
6+
dummy.next = head;
7+
8+
// Initialize two pointers
9+
let fast: ListNode | null = dummy;
10+
let slow: ListNode | null = dummy;
11+
12+
// Move fast pointer n+1 steps ahead
13+
for (let i = 0; i < n + 1; i++) {
14+
if (fast) {
15+
fast = fast.next;
16+
}
17+
}
18+
19+
// Move both pointers until fast reaches the end
20+
while (fast !== null) {
21+
fast = fast.next;
22+
slow = slow!.next;
23+
}
24+
25+
// Skip the nth node from the end
26+
if (slow && slow.next) {
27+
slow.next = slow.next.next;
28+
}
29+
30+
return dummy.next;
31+
}
32+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_61_remove_n_node_from_end_of_list import Solution, ListNode
4+
5+
class RemoveNNodeFromEndOfListTestCase(unittest.TestCase):
6+
7+
def create_linked_list(self, values):
8+
"""
9+
Helper function to create a linked list from a list of values.
10+
11+
:param values: List of node values
12+
:return: Head of the linked list
13+
"""
14+
if not values:
15+
return None
16+
17+
head = ListNode(values[0])
18+
current = head
19+
20+
for val in values[1:]:
21+
current.next = ListNode(val)
22+
current = current.next
23+
24+
return head
25+
26+
def linked_list_to_list(self, head):
27+
"""
28+
Helper function to convert linked list to Python list.
29+
30+
:param head: Head of the linked list
31+
:return: List of values
32+
"""
33+
result = []
34+
current = head
35+
36+
while current:
37+
result.append(current.val)
38+
current = current.next
39+
40+
return result
41+
42+
def test_first_pattern(self):
43+
# Example 1: Input: head = [1,2,3,4,5], n = 2
44+
# Output: [1,2,3,5]
45+
solution = Solution()
46+
head = self.create_linked_list([1, 2, 3, 4, 5])
47+
output = solution.removeNthFromEnd(head, 2)
48+
result = self.linked_list_to_list(output)
49+
target = [1, 2, 3, 5]
50+
self.assertEqual(result, target)
51+
52+
def test_second_pattern(self):
53+
# Example 2: Input: head = [1], n = 1
54+
# Output: []
55+
solution = Solution()
56+
head = self.create_linked_list([1])
57+
output = solution.removeNthFromEnd(head, 1)
58+
result = self.linked_list_to_list(output)
59+
target = []
60+
self.assertEqual(result, target)
61+
62+
def test_third_pattern(self):
63+
# Example 3: Input: head = [1,2], n = 1
64+
# Output: [1]
65+
solution = Solution()
66+
head = self.create_linked_list([1, 2])
67+
output = solution.removeNthFromEnd(head, 1)
68+
result = self.linked_list_to_list(output)
69+
target = [1]
70+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)