From b1a73ef7247aacc81a749502e0c28a2960708971 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 27 Jan 2026 04:19:49 -0600 Subject: [PATCH] adding algo --- .../ex_61_remove_n_node_from_end_of_list.py | 32 +++++++++ .../ex_61_remove_n_node_from_end_of_list.ts | 32 +++++++++ ...remove_n_node_from_end_of_list_round_22.py | 70 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_61_remove_n_node_from_end_of_list.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_61_remove_n_node_from_end_of_list.ts create mode 100644 tests/test_150_questions_round_22/test_61_remove_n_node_from_end_of_list_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_61_remove_n_node_from_end_of_list.py b/src/my_project/interviews/top_150_questions_round_22/ex_61_remove_n_node_from_end_of_list.py new file mode 100644 index 00000000..3d2e9e9f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_61_remove_n_node_from_end_of_list.py @@ -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 \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_61_remove_n_node_from_end_of_list.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_61_remove_n_node_from_end_of_list.ts new file mode 100644 index 00000000..2d1b4cfb --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_61_remove_n_node_from_end_of_list.ts @@ -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; +} + diff --git a/tests/test_150_questions_round_22/test_61_remove_n_node_from_end_of_list_round_22.py b/tests/test_150_questions_round_22/test_61_remove_n_node_from_end_of_list_round_22.py new file mode 100644 index 00000000..f0794e79 --- /dev/null +++ b/tests/test_150_questions_round_22/test_61_remove_n_node_from_end_of_list_round_22.py @@ -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) \ No newline at end of file