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
示例 1:
输入:head = [1,2,3,4,5], k = 2 输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4 输出:[2,0,1]
提示:
[0, 500]
-100 <= Node.val <= 100
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} */ // 闭合为环 var rotateRight = function(head, k) { if (k === 0 || !head || !head.next) { return head } let len = 1 let cur = head while (cur.next) { cur = cur.next len++; } cur.next = head k = len - k % len while (k--) { cur = cur.next } head = cur.next cur.next = null return head };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
61. 旋转链表
Description
Difficulty: 中等
Related Topics: 链表, 双指针
给你一个链表的头节点
head
,旋转链表,将链表每个节点向右移动k
个位置。示例 1:
示例 2:
提示:
[0, 500]
内-100 <= Node.val <= 100
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: