描述
Given a linked list, reverse the nodes of a linked list k at a time and return its modified 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 nodes, only nodes itself may be changed.
Only constant memory is allowed.
For 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
分析
分块翻转链表,将每 K 个节点作为一个链表翻转,翻转之后原来块的头结点作为尾节点指向链表中的下一个节点。
// Reverse Nodes in k-Group
// 递归版,时间复杂度O(n),空间复杂度O(1)
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k < 2)
return head;
ListNode next_group = head;
for (int i = 0; i < k; ++i) {
if (next_group != null)
next_group = next_group.next;
else
return head;
}
// next_group is the head of next group
// new_next_group is the new head of next group after reversion
ListNode new_next_group = reverseKGroup(next_group, k);
ListNode prev = null, cur = head;
while (cur != next_group) {
ListNode next = cur.next;
cur.next = prev != null ? prev : new_next_group;
prev = cur;
cur = next;
}
return prev; // prev will be the new head of this group
}
}
// Reverse Nodes in k-Group
// 迭代版,时间复杂度O(n),空间复杂度O(1)
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k < 2) return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
for(ListNode prev = dummy, end = head; end != null; end = prev.next) {
for (int i = 1; i < k && end != null; i++)
end = end.next;
if (end == null) break; // 不足 k 个
prev = reverse(prev, prev.next, end);
}
return dummy.next;
}
// prev 是 first 前一个元素, [begin, end] 闭区间,保证三者都不为 null
// 返回反转后的倒数第1个元素
ListNode reverse(ListNode prev, ListNode begin, ListNode end) {
ListNode end_next = end.next;
for (ListNode p = begin, cur = p.next, next = cur.next;
cur != end_next;
p = cur, cur = next, next = next != null ? next.next : null) {
cur.next = p;
}
begin.next = end_next;
prev.next = end;
return begin;
}
};