Skip to content

Commit 2bfedad

Browse files
committed
adding algo
1 parent c8ba9e3 commit 2bfedad

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
12+
# Create two dummy nodes for the two partitions
13+
less_dummy = ListNode(0)
14+
greater_dummy = ListNode(0)
15+
16+
# Pointers to build the two lists
17+
less = less_dummy
18+
greater = greater_dummy
19+
20+
# Traverse the original list
21+
current = head
22+
while current:
23+
if current.val < x:
24+
# Add to the "less than" list
25+
less.next = current
26+
less = less.next
27+
else:
28+
# Add to the "greater or equal" list
29+
greater.next = current
30+
greater = greater.next
31+
current = current.next
32+
33+
# Important: Set the end of greater list to None
34+
# to avoid cycles
35+
greater.next = None
36+
37+
# Connect the two lists
38+
less.next = greater_dummy.next
39+
40+
# Return the head of the combined list
41+
return less_dummy.next
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ListNode } from './ListNode';
2+
3+
function partition(head: ListNode | null, x: number): ListNode | null {
4+
// Create two dummy nodes for the two partitions
5+
const lessDummy = new ListNode(0);
6+
const greaterDummy = new ListNode(0);
7+
8+
// Pointers to build the two lists
9+
let less = lessDummy;
10+
let greater = greaterDummy;
11+
12+
// Traverse the original list
13+
let current = head;
14+
while (current !== null) {
15+
if (current.val < x) {
16+
// Add to the "less than" list
17+
less.next = current;
18+
less = less.next;
19+
} else {
20+
// Add to the "greater or equal" list
21+
greater.next = current;
22+
greater = greater.next;
23+
}
24+
current = current.next;
25+
}
26+
27+
// Important: Set the end of greater list to null
28+
// to avoid cycles
29+
greater.next = null;
30+
31+
// Connect the two lists
32+
less.next = greaterDummy.next;
33+
34+
// Return the head of the combined list
35+
return lessDummy.next;
36+
}
37+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_64_partition_linked_list import Solution, ListNode
4+
5+
class PartitionLinkedListTestCase(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,4,3,2,5,2], x = 3
44+
# Output: [1,2,2,4,3,5]
45+
solution = Solution()
46+
head = self.create_linked_list([1, 4, 3, 2, 5, 2])
47+
output = solution.partition(head, 3)
48+
result = self.linked_list_to_list(output)
49+
target = [1, 2, 2, 4, 3, 5]
50+
self.assertEqual(result, target)
51+
52+
def test_second_pattern(self):
53+
# Example 2: Input: head = [2,1], x = 2
54+
# Output: [1,2]
55+
solution = Solution()
56+
head = self.create_linked_list([2, 1])
57+
output = solution.partition(head, 2)
58+
result = self.linked_list_to_list(output)
59+
target = [1, 2]
60+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)