diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_64_partition_linked_list.py b/src/my_project/interviews/top_150_questions_round_22/ex_64_partition_linked_list.py new file mode 100644 index 00000000..2016a3ba --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_64_partition_linked_list.py @@ -0,0 +1,41 @@ +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 partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]: + # Create two dummy nodes for the two partitions + less_dummy = ListNode(0) + greater_dummy = ListNode(0) + + # Pointers to build the two lists + less = less_dummy + greater = greater_dummy + + # Traverse the original list + current = head + while current: + if current.val < x: + # Add to the "less than" list + less.next = current + less = less.next + else: + # Add to the "greater or equal" list + greater.next = current + greater = greater.next + current = current.next + + # Important: Set the end of greater list to None + # to avoid cycles + greater.next = None + + # Connect the two lists + less.next = greater_dummy.next + + # Return the head of the combined list + return less_dummy.next \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_64_partition_linked_list.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_64_partition_linked_list.ts new file mode 100644 index 00000000..5b089a83 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_64_partition_linked_list.ts @@ -0,0 +1,37 @@ +import { ListNode } from './ListNode'; + +function partition(head: ListNode | null, x: number): ListNode | null { + // Create two dummy nodes for the two partitions + const lessDummy = new ListNode(0); + const greaterDummy = new ListNode(0); + + // Pointers to build the two lists + let less = lessDummy; + let greater = greaterDummy; + + // Traverse the original list + let current = head; + while (current !== null) { + if (current.val < x) { + // Add to the "less than" list + less.next = current; + less = less.next; + } else { + // Add to the "greater or equal" list + greater.next = current; + greater = greater.next; + } + current = current.next; + } + + // Important: Set the end of greater list to null + // to avoid cycles + greater.next = null; + + // Connect the two lists + less.next = greaterDummy.next; + + // Return the head of the combined list + return lessDummy.next; +} + diff --git a/tests/test_150_questions_round_22/test_64_partition_linked_list_round_22.py b/tests/test_150_questions_round_22/test_64_partition_linked_list_round_22.py new file mode 100644 index 00000000..06c0aab7 --- /dev/null +++ b/tests/test_150_questions_round_22/test_64_partition_linked_list_round_22.py @@ -0,0 +1,60 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_64_partition_linked_list import Solution, ListNode + +class PartitionLinkedListTestCase(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,4,3,2,5,2], x = 3 + # Output: [1,2,2,4,3,5] + solution = Solution() + head = self.create_linked_list([1, 4, 3, 2, 5, 2]) + output = solution.partition(head, 3) + result = self.linked_list_to_list(output) + target = [1, 2, 2, 4, 3, 5] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Example 2: Input: head = [2,1], x = 2 + # Output: [1,2] + solution = Solution() + head = self.create_linked_list([2, 1]) + output = solution.partition(head, 2) + result = self.linked_list_to_list(output) + target = [1, 2] + self.assertEqual(result, target) \ No newline at end of file