LeetCode025 Reverse Nodes in K-Group
reverse nodes by K as a group
first, let’s try to use recursion method.
the following code has two parts, reverseKGroup() and reverseLinkedList(). the first one is the recursion function, and the second one is a tool function which reverse a linkedlist by first k elements, we don;t do it in place, instead, we create a new linkedlist to store the reversed part.
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null) return null;
if(k == 0 || k== 1) return head;
int count = 0;
ListNode p = head;
while (count < k && p != null) { //if from head to tail, we have
p = p.next;
count++;
}
if (count == k) { //if this remaining chunk has the size of k, then we can do
ListNode reverseHead = reverseLinkedList(head, k); //head is moved to the last position of linkedlist
head.next = reverseKGroup(p, k);//so that's why we use head.next to link the following
return reverseHead; //if coount == k, then we return the reversedhead
}
return head; //if count is not enought for k, then we only needs to return current head
}
private ListNode reverseLinkedList(ListNode head, int k) { //we need to use this method to reverse k nodes of the given linkedlist
ListNode new_head = null; //create a new linkedlist to store reversed current linkedlist
ListNode p = head;
while (k > 0) {
ListNode next_node = p.next;
p.next = new_head;
new_head = p;
p = next_node;
k--;
}
return new_head; //return the new created linkedlist's head
}
}
and then, we will use iterative way:
and the reverse LinkedList tool function is not changed.
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null) return null;
if(k == 0 || k== 1) return head;
ListNode p = head; //p is the pointer to indicates the parts we already proceed in the whole original linkedlist
//ktail is the tail node of the previous set of k nodes after reversal.
ListNode ktail = null;
//acts as the head of the final list that we need to return as the output. Basically, this is the k^{th}k
//th node from the beginning of the original list.
ListNode new_head = null;
while (p != null) {
int count = 0; //k chunk counter
p = head;
while (count < k && p != null) {
p = p.next;
count++;
}
if (count == k) {
ListNode revHead = reverseLinkedList(head, k); //new head
if (new_head == null) new_head = revHead; //we only update new_head once, because we need to preserve the head.
if (ktail != null) ktail.next = revHead;
ktail = head; //ktail moves to the tail of sorted part
head = p; //and head move to the head of unsorted part
}
}
//if the remaining part is not enough for k, then we just attached the remaining linkedlist
if (ktail != null) {
ktail.next = head;
}
return new_head;
}
private ListNode reverseLinkedList(ListNode head, int k) { //we need to use this method to reverse k nodes of the given linkedlist
ListNode new_head = null; //create a new linkedlist to store reversed current linkedlist
ListNode p = head;
while (k > 0) {
ListNode next_node = p.next;
p.next = new_head;
new_head = p;
p = next_node;
k--;
}
return new_head; //return the new created linkedlist's head
}
}

浙公网安备 33010602011771号