Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
# Check if we have k nodes to reverse
curr = head
count = 0
while curr and count < k:
curr = curr.next
count += 1

# If we have k nodes, reverse them
if count == k:
# Reverse k nodes
prev = None
curr = head
for _ in range(k):
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node

# head is now the last node in reversed group
# prev is the new head of this group
# curr is the start of next group
# Recursively reverse next groups
head.next = self.reverseKGroup(curr, k)
return prev

# Less than k nodes remaining, return as is
return head
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ListNode } from './ListNode';

function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
// Check if we have k nodes to reverse
let curr = head;
let count = 0;
while (curr && count < k) {
curr = curr.next;
count++;
}

// If we have k nodes, reverse them
if (count === k) {
// Reverse k nodes
let prev: ListNode | null = null;
curr = head;
for (let i = 0; i < k; i++) {
const nextNode = curr!.next;
curr!.next = prev;
prev = curr;
curr = nextNode;
}

// head is now the last node in reversed group
// prev is the new head of this group
// curr is the start of next group
// Recursively reverse next groups
head!.next = reverseKGroup(curr, k);
return prev;
}

// Less than k nodes remaining, return as is
return head;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_60_reverse_node_in_k_group import Solution, ListNode

class ReverseNodeInKGroupTestCase(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], k = 2
# Output: [2,1,4,3,5]
solution = Solution()
head = self.create_linked_list([1, 2, 3, 4, 5])
output = solution.reverseKGroup(head, 2)
result = self.linked_list_to_list(output)
target = [2, 1, 4, 3, 5]
self.assertEqual(result, target)

def test_second_pattern(self):
# Example 2: Input: head = [1,2,3,4,5], k = 3
# Output: [3,2,1,4,5]
solution = Solution()
head = self.create_linked_list([1, 2, 3, 4, 5])
output = solution.reverseKGroup(head, 3)
result = self.linked_list_to_list(output)
target = [3, 2, 1, 4, 5]
self.assertEqual(result, target)