[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个元素。在该点将链表分成两个部分,然后调换顺序即可。

陷阱:

k的长度没有说明,可能k比链表的长度还要大。 设链表的长度为Len,那么移动的节点个数应该为 K%Len.

  

class Solution {
public:
    int getListLength(ListNode *head){
        int len=0;
        ListNode *tmp=head;
        while(tmp){
            len++;
            tmp = tmp->next;
        }
        return len;
    }
    ListNode *rotateRight(ListNode *head, int x) {
        if(head==NULL) return head;  // empty list
        int len = getListLength(head);
        x = x%len; // how many nodes to rotate
        if( len<=1 || x==0) return head;

       // find xth node from tail of list
        ListNode *tmp=head;
        ListNode *lend=head;
        ListNode *hstart=head;
        while(tmp->next){
            if(x==0){
                lend = lend->next;
            }else{
                --x;
            }
            tmp = tmp->next;
        }

        hstart = lend->next;
        lend->next=NULL;
        tmp->next=head;
        return hstart;
    }
};

转载请注明出处: http://www.cnblogs.com/double-win/谢谢!

[LeetCode 题解]: Rotate List,布布扣,bubuko.com

时间: 2024-12-24 22:40:33

[LeetCode 题解]: Rotate List的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

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 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

【Leetcode】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? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod