diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_59_reverse_linked_list_ii.py b/src/my_project/interviews/top_150_questions_round_22/ex_59_reverse_linked_list_ii.py new file mode 100644 index 00000000..7ec328d2 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_59_reverse_linked_list_ii.py @@ -0,0 +1,39 @@ +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 reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]: + if not head or left == right: + return head + + # Create a dummy node to handle edge cases where left = 1 + dummy = ListNode(0) + dummy.next = head + + # Step 1: Move to the node before the left position + prev = dummy + for _ in range(left - 1): + prev = prev.next + + # Step 2: Reverse the sublist from left to right + # prev points to the node before left position + # current points to the left position + current = prev.next + + # Reverse nodes from left to right + for _ in range(right - left): + # Save the next node to move + next_node = current.next + # Point current.next to the node after next_node + current.next = next_node.next + # Insert next_node right after prev + next_node.next = prev.next + prev.next = next_node + + return dummy.next \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_59_reverse_linked_list_ii.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_59_reverse_linked_list_ii.ts new file mode 100644 index 00000000..92ee7660 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_59_reverse_linked_list_ii.ts @@ -0,0 +1,36 @@ +import { ListNode } from './ListNode'; + +function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null { + if (!head || left === right) { + return head; + } + + // Create a dummy node to handle edge cases where left = 1 + const dummy = new ListNode(0); + dummy.next = head; + + // Step 1: Move to the node before the left position + let prev: ListNode | null = dummy; + for (let i = 0; i < left - 1; i++) { + prev = prev!.next; + } + + // Step 2: Reverse the sublist from left to right + // prev points to the node before left position + // current points to the left position + let current: ListNode | null = prev!.next; + + // Reverse nodes from left to right + for (let i = 0; i < right - left; i++) { + // Save the next node to move + const nextNode: ListNode | null = current!.next; + // Point current.next to the node after nextNode + current!.next = nextNode!.next; + // Insert nextNode right after prev + nextNode!.next = prev!.next; + prev!.next = nextNode; + } + + return dummy.next; +} + diff --git a/tests/test_150_questions_round_22/test_59_reverse_linked_list_ii_round_22.py b/tests/test_150_questions_round_22/test_59_reverse_linked_list_ii_round_22.py new file mode 100644 index 00000000..03cc0cec --- /dev/null +++ b/tests/test_150_questions_round_22/test_59_reverse_linked_list_ii_round_22.py @@ -0,0 +1,60 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_59_reverse_linked_list_ii import Solution, ListNode + +class ReverseLinkedListsIITestCase(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], left = 2, right = 4 + # Output: [1,4,3,2,5] + solution = Solution() + head = self.create_linked_list([1, 2, 3, 4, 5]) + output = solution.reverseBetween(head, 2, 4) + result = self.linked_list_to_list(output) + target = [1, 4, 3, 2, 5] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Example 2: Input: head = [5], left = 1, right = 1 + # Output: [5] + solution = Solution() + head = self.create_linked_list([5]) + output = solution.reverseBetween(head, 1, 1) + result = self.linked_list_to_list(output) + target = [5] + self.assertEqual(result, target) \ No newline at end of file