61. Rotate List(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.

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(!head) return head;

        ListNode *p = head;
        ListNode *newHead;
        int num = 1;
        while(p->next)
        {
            p = p->next;
            num++;
        }
        k = k%num;
        if(k==0) return head;
        p->next = head;
        p = head;
        for(int i = 0; i<num-k-1; i++)
        {
            p = p->next;
        }
        head = p->next;
        p->next = NULL;
        return head;
    }
};
时间: 2024-10-13 22:27:31

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

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

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

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

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. 此题先想需要多少个ListNode,dummy,用于返回.fast,slow.三个就够了,我开始做的时候,多用了一个cur来计算链表长度

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 解答: 不是很懂这道题的通过率

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

一天一道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以后的节点移动到前面来,与头结点