LeetCode OJ 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.

Subscribe to see which companies asked this question

解答:

不是很懂这道题的通过率为何如此之低,其实只要考虑三种边界情况就可以了,一是空链表的时候,二是k是链表节点数整数倍的时候,三是k大于链表节点数的时候……

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k) {
    int count = 0, i;
    struct ListNode *pNode = head, *tmp;

    if(NULL == head)
        return;
    while(NULL != pNode){
        count++;
        pNode = pNode->next;
    }
    pNode = head;
    for(i = 0; i < count - k % count - 1; i++){
        pNode = pNode->next;
    }
    tmp = pNode->next;
    pNode->next = NULL;
    if(NULL == tmp)
        return head;
    pNode = tmp;
    while(NULL != pNode->next){
        pNode = pNode->next;
    }
    pNode->next = head;
    return tmp;
}
时间: 2024-10-06 01:15:18

LeetCode OJ 61. Rotate List的相关文章

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

&lt;LeetCode OJ&gt; 48. Rotate Image

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium 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? Subscribe to see which companies asked this

【一天一道LeetCode】#61. Rotate List

一天一道LeetCode系列 (一)题目 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. (二)解题 本题的思路: 1.找到倒数第K个节点 2.将k以后的节点移动到前面来,与头结点

LeetCode OJ: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]. 将数组的内容倒置,看例子就知道是什么意思: 1 class Solution { 2 public: 3 void rotate(vector<int>& nums, int k) { 4 if(

笔试题74. LeetCode OJ (61)

    Rotate List    这个题的意思旋转链表,更具体点的意思右移链表,移出去的节点放到头部前面,结合着题目给出的例子还是很好理解的. 这个题的主要思路是:摘取从末尾到头的k个节点,然后将他们放到头部. 需要注意的是,上面说的k并不一定等于传入的k的值,因为这个k很可能比链表的长度还大.所以我主要思路是:遍历一遍链表,找到链表的长度n,然后k%=n(这时候k<n,我们更喜欢的是此时k=0),这样就可以找出实际需要移动的节点的个数,然后将链表的最后k个节点放到链表的前面就行了.大概方法

LeetCode OJ 189. 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

【Leetcode】61. Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. Example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. Tips:右移结点,过程如下: k=2,右移两次: ①5->1->2->3->4 ②4->5->1-&g

LeetCode OJ Linked List: 24题、148题和61题

题外话:最近打算做一些LeetCode OJ上面的练习题了,当然很多前辈都已经写过若干解题报告了.坦白说,也正是因为前辈们的贡献,才让我们学习到了很多知识.所以,我一直都在犹豫到底要不要写解题报告多此一举呢?当然,我水平很渣啊.我觉得虽然,有很多很好的解题报告可参考了,但是自己对待这件事的态度又是另外一回事,记录下来,完全是提醒自己这段时间是有计划的,是要做题的. 24题:Swap Nodes in Pairs 题目分析:链表长度为奇数时,最后一个节点不用再调整啦.当然了,不能改变节点的值,否则

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个节点放到前面,可以采用快慢指针处理.不