0025. Reverse Nodes in k-Group (H)
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)\)的空间。
思路
整体上与 24. Swap Nodes in Pairs (M) 的解决方法相似,只是多了2个以上结点的逆序排列,因为只能使用常数的额外空间,所以不能用栈来实现逆序,直接使用插入到头结点之前的方式来实现逆序。
代码实现
Java
递归
/**
* 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;
}
}
JavaScript
递归
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup = function (head, k) {
if (head === null) {
return null
}
let nextHead = head
for (let i = 1; i <= k; i++) {
if (nextHead === null) {
return head
}
nextHead = nextHead.next
}
let newHead = null
let tail = head
while (head !== nextHead) {
let tmp = head.next
head.next = newHead
newHead = head
head = tmp
}
tail.next = reverseKGroup(nextHead, k)
return newHead
}
迭代
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var reverseKGroup = function (head, k) {
let len = 0
let p = head
while (p !== null) {
len++
p = p.next
}
let dummy = new ListNode(0, head)
let tail = dummy
p = head
while (len >= k) {
let nextTail = p
let newHead = null
for (let i = 0; i < k; i++) {
let tmp = p.next
p.next = newHead
newHead = p
p = tmp
}
tail.next = newHead
tail = nextTail
len -= k
}
tail.next = p
return dummy.next
}