LeetCode 0025. Reverse Nodes in k-Group Solution Java | Python | C/C++ | JavaScripts | Go | Rust | Explaination + CODE

CoderIndeed
0
0025. Reverse Nodes in k-Group

Description

Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.

You may not alter the values in the list's nodes, only nodes themselves may be changed.

 

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]

Example 2:

Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]

 

Constraints:

  • The number of nodes in the list is n.
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

 

Follow-up: Can you solve the problem in O(1) extra memory space?

Solutions

Solution 1: Simulation

We can simulate the entire reversal process according to the problem description.

First, we define a helper function reverse to reverse a linked list. Then, we define a dummy head node dummy and set its next pointer to head.

Next, we traverse the linked list, processing k nodes at a time. If the remaining nodes are fewer than k, we do not perform the reversal. Otherwise, we extract k nodes and call the reverse function to reverse these k nodes. Then, we connect the reversed linked list back to the original linked list. We continue to process the next k nodes until the entire linked list is traversed.

The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).

PythonJavaGoTypeScriptRustC#PHP
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: def reverse(head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode() cur = head while cur: nxt = cur.next cur.next = dummy.next dummy.next = cur cur = nxt return dummy.next dummy = pre = ListNode(next=head) while pre: cur = pre for _ in range(k): cur = cur.next if cur is None: return dummy.next node = pre.next nxt = cur.next cur.next = None pre.next = reverse(node) node.next = nxt pre = node return dummy.next(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !