Untitled

 avatar
unknown
plain_text
2 months ago
1.2 kB
5
Indexable
Reverse LL in group of given size K
Given the head of a singly linked list containing integers, reverse the nodes of the list in groups of k and return the head of the modified list. If the number of nodes is not a multiple of k, then the remaining nodes at the end should be kept as is and not reversed.
Do not change the values of the nodes, only change the links between nodes.

Example 1
Input: head -> 1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: head -> 2 -> 1 -> 4 -> 3 -> 5
Explanation: The groups 1 -> 2 and 3 -> 4 were reversed as 2 -> 1 and 4 -> 3.

Example 2
Input: head -> 1 -> 2 -> 3 -> 4 -> 5, k = 3
Output: head -> 3 -> 2 -> 1 -> 4 -> 5
Explanation: The groups 1 -> 2 -> 3 were reversed as 3 -> 2 -> 1.
Note that 4 -> 5 was not reversed.

/*Definition of singly linked list:
class ListNode {
    int val;
    ListNode next;

    ListNode() {
        val = 0;
        next = null;
    }

    ListNode(int data1) {
        val = data1;
        next = null;
    }

    ListNode(int data1, ListNode next1) {
        val = data1;
        next = next1;
    }
}
 */

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {

    }
}
Editor is loading...
Leave a Comment