Reverse Nodes in k-Group (H)
Given a linked list, reverse the nodes of a linked list k at a time and return its 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.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
- Only constant extra memory is allowed.
- You may not alter the values in the list’s nodes, only nodes itself may be changed.
题意
将给定链表中的每k个结点按逆序重新排列,不能改变结点内的值,只能改动结点本身,且只能使用 O ( 1 ) O(1) O(1)的空间。
思路
整体上与 24. Swap Nodes in Pairs (M) 的解决方法相似,只是多了2个以上结点的逆序排列,因为只能使用常数的额外空间,所以不能用栈来实现逆序,直接使用插入到头结点之前的方式来实现逆序。
代码实现 - 递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode pointer = head;
// 判断剩余结点数是否达到k,并找到下一个k元组的头结点
for (int i = 0; i < k; i++) {
if (pointer == null) {
return head;
}
pointer = pointer.next;
}
ListNode nextHead = pointer; // 记录下一个k元组的头结点
ListNode first = null; // 逆序排列时记录逆序链表的头结点
pointer = head;
// 逆序处理
while (pointer != nextHead) {
ListNode temp = pointer.next;
pointer.next = first;
first = pointer;
pointer = temp;
}
head.next = reverseKGroup(nextHead, k);
return first;
}
}
代码实现 - 非递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode ans = new ListNode(0);
ans.next = head;
ListNode root = ans; // 记录上一个k元组逆序后的最后一个结点
// 统计结点数
ListNode pointer = head;
int num = 0;
while (pointer != null) {
num++;
pointer = pointer.next;
}
pointer = ans.next;
while (num >= k) {
ListNode first = null; // 逆序时的头结点
ListNode nextRoot = pointer; // 记录当前k元组逆序前的第一个结点
// 逆序处理
for (int i = 0; i < k; i++) {
ListNode temp = pointer.next;
pointer.next = first;
first = pointer;
pointer = temp;
}
root.next = first;
root = nextRoot;
num -= k;
}
root.next = pointer; // 将个数不足k的结点补充到链表最后
return ans.next;
}
}