Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
# Create a dummy node to handle edge cases (e.g., removing the head)
dummy = ListNode(0)
dummy.next = head

# Initialize two pointers
fast = dummy
slow = dummy

# Move fast pointer n+1 steps ahead
for _ in range(n + 1):
fast = fast.next

# Move both pointers until fast reaches the end
while fast:
fast = fast.next
slow = slow.next

# Skip the nth node from the end
slow.next = slow.next.next

return dummy.next
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ListNode } from './ListNode';

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// Create a dummy node to handle edge cases (e.g., removing the head)
const dummy = new ListNode(0);
dummy.next = head;

// Initialize two pointers
let fast: ListNode | null = dummy;
let slow: ListNode | null = dummy;

// Move fast pointer n+1 steps ahead
for (let i = 0; i < n + 1; i++) {
if (fast) {
fast = fast.next;
}
}

// Move both pointers until fast reaches the end
while (fast !== null) {
fast = fast.next;
slow = slow!.next;
}

// Skip the nth node from the end
if (slow && slow.next) {
slow.next = slow.next.next;
}

return dummy.next;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_61_remove_n_node_from_end_of_list import Solution, ListNode

class RemoveNNodeFromEndOfListTestCase(unittest.TestCase):

def create_linked_list(self, values):
"""
Helper function to create a linked list from a list of values.

:param values: List of node values
:return: Head of the linked list
"""
if not values:
return None

head = ListNode(values[0])
current = head

for val in values[1:]:
current.next = ListNode(val)
current = current.next

return head

def linked_list_to_list(self, head):
"""
Helper function to convert linked list to Python list.

:param head: Head of the linked list
:return: List of values
"""
result = []
current = head

while current:
result.append(current.val)
current = current.next

return result

def test_first_pattern(self):
# Example 1: Input: head = [1,2,3,4,5], n = 2
# Output: [1,2,3,5]
solution = Solution()
head = self.create_linked_list([1, 2, 3, 4, 5])
output = solution.removeNthFromEnd(head, 2)
result = self.linked_list_to_list(output)
target = [1, 2, 3, 5]
self.assertEqual(result, target)

def test_second_pattern(self):
# Example 2: Input: head = [1], n = 1
# Output: []
solution = Solution()
head = self.create_linked_list([1])
output = solution.removeNthFromEnd(head, 1)
result = self.linked_list_to_list(output)
target = []
self.assertEqual(result, target)

def test_third_pattern(self):
# Example 3: Input: head = [1,2], n = 1
# Output: [1]
solution = Solution()
head = self.create_linked_list([1, 2])
output = solution.removeNthFromEnd(head, 1)
result = self.linked_list_to_list(output)
target = [1]
self.assertEqual(result, target)