Reverse Nodes in k-Group LeetCode Solution

Last updated on October 10th, 2024 at 02:02 am

Here, We see Reverse Nodes in k-Group LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Topics

Linked List

Companies

Facebook, Microsoft

Level of Question

Hard

Reverse Nodes in k-Group LeetCode Solution

Reverse Nodes in k-Group LeetCode Solution

Problem Statement

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 the nodes themselves may be changed.

Reverse Nodes in k-Group ex1
fig-1
Example 1: (fig-1)
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Reverse Nodes in k Group
fig-2
Example 2: (fig-2)
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]

1. Reverse Nodes in k-Group Leetcode Solution C++

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* temp=head;
        for(int i=0;i<k;i++){
            if(!temp)return head;
            temp=temp->next;
        }
        
        ListNode *prev=NULL;
        ListNode *nex1=NULL;
        temp=head;
        for(int i=0;i<k;i++){
            nex1=temp->next;
            temp->next=prev;
            prev=temp;
            temp=nex1;
        }
        if(nex1!=NULL)
            head->next=reverseKGroup(nex1,k);
        return prev;        
    }
};

2. Reverse Nodes in k-Group Leetcode Solution Java

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
    ListNode prev = null;
    ListNode current = head;
    int count = 0;
    ListNode next = null;
	
    //reverse blindly
    while (current != null && count < k) {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
        count++;
    }

    //restoring if count lesser
    if (count < k) {
        current = prev;
        prev = null;
        next = null;
		
        while (count != 0) {
            count--;
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
    }
    if (next != null) {
        head.next = reverseKGroup(next, k);
    }
    return prev;        
    }
}

3. Reverse Nodes in k-Group Leetcode Solution JavaScript

var reverseKGroup = function(head, k) {
    return reverseAGroup(head);
    
    function reverseAGroup(start) {
        let temp_k = k;
        let curr = start;
        while(curr && temp_k-- > 0) {
            curr = curr.next;
        }
        if(temp_k > 0) {
            return start;
        }
        const groupTail = start, 
              groupHead = reverse(start, curr); 
        
        if(curr) {  
            groupTail.next = reverseAGroup(curr); 
        }
        return groupHead; 
    }
    function reverse(root, nextGroup) {
        let curr = root,
            next = null,
            prev = null;
        
        while(curr && curr !== nextGroup) {  
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev; 
    }    
};

4. Reverse Nodes in k-Group Leetcode Solution Python

class Solution(object):
    def reverseKGroup(self, head, k):
        l, node = 0, head
        while node:
            l += 1
            node = node.next
        if k <= 1 or l < k:
            return head
        node, cur = None, head
        for _ in xrange(k):
            nxt = cur.next
            cur.next = node
            node = cur
            cur = nxt
        head.next = self.reverseKGroup(cur, k)
        return node
Scroll to Top