We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 困难
Related Topics: 递归, 链表
给你链表的头节点 head ,每 k个节点一组进行翻转,请你返回修改后的链表。
head
k
k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k的整数倍,那么请将最后剩余的节点保持原有顺序。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例 1:
输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5]
示例 2:
输入:head = [1,2,3,4,5], k = 3 输出:[3,2,1,4,5]
提示:
n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
**进阶:**你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?
O(1)
Language: 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} */ const myReverse = (head, tail) => { let prev = tail.next; let p = head; while (prev !== tail) { const nex = p.next; p.next = prev; prev = p; p = nex; } return [tail, head]; } var reverseKGroup = function(head, k) { const hair = new ListNode(0) hair.next = head let pre = hair while (head) { let tail = pre // 查看剩余部分长度是否大于等于k for (let i = 0; i < k; ++i) { tail = tail.next if (!tail) { return hair.next } } const nex = tail.next; [head, tail] = myReverse(head, tail) // 把子链表重新接回原链表 pre.next = head tail.next = nex pre = tail head = tail.next } return hair.next };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
25. K 个一组翻转链表
Description
Difficulty: 困难
Related Topics: 递归, 链表
给你链表的头节点
head
,每k
个节点一组进行翻转,请你返回修改后的链表。k
是一个正整数,它的值小于或等于链表的长度。如果节点总数不是k
的整数倍,那么请将最后剩余的节点保持原有顺序。你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例 1:
示例 2:
提示:
n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
**进阶:**你可以设计一个只用
O(1)
额外内存空间的算法解决此问题吗?Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: