Skip to content

Commit aceb78e

Browse files
authored
Merge pull request #1528 from ivanpenaloza/january26
adding algo
2 parents aa8b2b1 + fa061a3 commit aceb78e

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
12+
# Check if we have k nodes to reverse
13+
curr = head
14+
count = 0
15+
while curr and count < k:
16+
curr = curr.next
17+
count += 1
18+
19+
# If we have k nodes, reverse them
20+
if count == k:
21+
# Reverse k nodes
22+
prev = None
23+
curr = head
24+
for _ in range(k):
25+
next_node = curr.next
26+
curr.next = prev
27+
prev = curr
28+
curr = next_node
29+
30+
# head is now the last node in reversed group
31+
# prev is the new head of this group
32+
# curr is the start of next group
33+
# Recursively reverse next groups
34+
head.next = self.reverseKGroup(curr, k)
35+
return prev
36+
37+
# Less than k nodes remaining, return as is
38+
return head
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ListNode } from './ListNode';
2+
3+
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
4+
// Check if we have k nodes to reverse
5+
let curr = head;
6+
let count = 0;
7+
while (curr && count < k) {
8+
curr = curr.next;
9+
count++;
10+
}
11+
12+
// If we have k nodes, reverse them
13+
if (count === k) {
14+
// Reverse k nodes
15+
let prev: ListNode | null = null;
16+
curr = head;
17+
for (let i = 0; i < k; i++) {
18+
const nextNode = curr!.next;
19+
curr!.next = prev;
20+
prev = curr;
21+
curr = nextNode;
22+
}
23+
24+
// head is now the last node in reversed group
25+
// prev is the new head of this group
26+
// curr is the start of next group
27+
// Recursively reverse next groups
28+
head!.next = reverseKGroup(curr, k);
29+
return prev;
30+
}
31+
32+
// Less than k nodes remaining, return as is
33+
return head;
34+
}
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_60_reverse_node_in_k_group import Solution, ListNode
4+
5+
class ReverseNodeInKGroupTestCase(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,2,3,4,5], k = 2
44+
# Output: [2,1,4,3,5]
45+
solution = Solution()
46+
head = self.create_linked_list([1, 2, 3, 4, 5])
47+
output = solution.reverseKGroup(head, 2)
48+
result = self.linked_list_to_list(output)
49+
target = [2, 1, 4, 3, 5]
50+
self.assertEqual(result, target)
51+
52+
def test_second_pattern(self):
53+
# Example 2: Input: head = [1,2,3,4,5], k = 3
54+
# Output: [3,2,1,4,5]
55+
solution = Solution()
56+
head = self.create_linked_list([1, 2, 3, 4, 5])
57+
output = solution.reverseKGroup(head, 3)
58+
result = self.linked_list_to_list(output)
59+
target = [3, 2, 1, 4, 5]
60+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)