19.2.9 [LeetCode 61] Rotate List

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

题意

把链表循环右移k个

题解

 1 class Solution {
 2 public:
 3     ListNode* rotateRight(ListNode* head, int k) {
 4         if (head == NULL)return NULL;
 5         ListNode*pre, *p = head;
 6         int cnt = 0;
 7         while (p != NULL) {
 8             p = p->next;
 9             cnt++;
10         }
11         k %= cnt;
12         if (k == 0)return head;
13         p = head;
14         while (k--)
15             p = p->next;
16         pre = head;
17         while (p->next != NULL) {
18             p = p->next;
19             pre = pre->next;
20         }
21         ListNode*ans = pre->next;
22         pre->next = NULL;
23         p->next = head;
24         return ans;
25     }
26 };

考虑空链和不移动的特殊情况

原文地址:https://www.cnblogs.com/yalphait/p/10357610.html

时间: 2024-11-05 22:03:32

19.2.9 [LeetCode 61] Rotate List的相关文章

LeetCode --- 61. Rotate List

题目链接:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

leetcode 61 Rotate List ----- java

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题目意思不难,就是说给一个数k,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len

[LeetCode] 61. Rotate List 解题思路

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 问题:给定列表 和一个整数 k ,旋转列表最后 k 个元素至列表最前面. 关键是找到最后元素 lastOne 和 旋转后列表新的最后元素

leetCode 61.Rotate List (旋转链表) 解题思路和方法

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 思路:题目很清晰,思路是先得到链表长度,再从头开始直到特定点,开始变换连接即可. 代码如下: /** * D

19.2.7 [LeetCode 48] Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the

[leetcode]61. Rotate List反转链表k个节点

类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null||head.next==null||k==0) return head; int len = 0; ListNode p = head; //计算链表长度,防止k大于长度 while (p!=null) { len++; p = p.next; } //k大于等于len的情况 k = k>=len

61. Rotate List(M);19. Remove Nth Node From End of List(M)

61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. Total Accepted: 102574 Total Submissions: 42333

LeetCode OJ 61. Rotate List 考虑边界条件

题目链接:https://leetcode.com/problems/rotate-list/ 61. Rotate List My Submissions QuestionEditorial Solution Total Accepted: 71917 Total Submissions: 311425 Difficulty: Medium Given a list, rotate the list to the right by k places, where k is non-negati