LeetCode: Rotate List [060]

【题目】

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.

【题意】

给定一个链表L,和非负数K,要求把链表的后k个节点移到链表前

【思路】

先将指针指向指向倒数第K个节点,然后反转即可

注意

1. k>链表长度的特殊情况

2. k=链表长度的特殊情况, k=k%len, len为链表长度

3. k=0;

【代码】

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(head==NULL || k<=0)return head;
        int len=0;
        ListNode*p=head;
        //获得链表长度
        while(p){len++;p=p->next;}
        //更新K值,因为K有可能比链表长度大,因此需要做取余操作
        k=k%len;
        if(k==0)return head;
        //确定倒数第k个节点
        ListNode*p1=head;
        ListNode*p2=head;
        int step=0;
        //p2先向前移动k个节点
        while(step<k && p2){step++; p2=p2->next;}
        //p1,p2同时移动,直至p2移动到链表的最后一个节点,此时p1指向的是倒数第k个节点的前一个节点。
        while(p2->next){
            p2=p2->next;
            p1=p1->next;
        }
        p2->next=head;
        head=p1->next;
        p1->next=NULL;
        return head;
    }
};

LeetCode: Rotate List [060]

时间: 2024-10-09 05:02:49

LeetCode: Rotate List [060]的相关文章

Leetcode: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. 解题分析: 不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转 对于链表的旋转,实际上

[LeetCode] Rotate Image [26]

题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 原题链接(点我) 解题思路 顺时针方向旋转数组90°.这个题也是个没啥意思的题,自己画画图,找找规律.就出来了.我举一个n=4的例子还说明下规律: 通过图可以看出A[0][0] = A[3][0],.....从这些中

LeetCode: Rotate Image [047]

[题目] You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? [题意] 给定一个nXn的二维矩阵,按时钟方向旋转90度,不能使用额外的数据结构 [思路] 从外向内逐层旋转 [代码] class Solution { public: void rotateMatrix(vec

[LeetCode] Rotate Array / List

Question: http://leetcode.com/2010/04/rotating-array-in-place.html Rotate a one-dimensional array of n elements to the right by k steps.  For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d} // O (n) pub

[leetcode]Rotate Image @ Python

原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 解题思路1:首先沿着副对角线翻转一次,然后沿着水平中线翻转一次,就可以得到所要求的矩阵了.时间复杂度O(n^2)

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/rotate-list/ 得到链表长度,链表指向尾节点,

[LeetCode]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大于或等于链表的长度n,所以实际效果等同于循环右移k%n次. 遍历单链

[LeetCode] Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro