Total Accepted: 55428 Total Submissions: 250727 Difficulty: Medium

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. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. int getListLength(ListNode* head)
  12. {
  13. int len = ;
  14. while(head){
  15. len++;
  16. head = head->next;
  17. }
  18. return len;
  19. }
  20. ListNode* rotateRight(ListNode* head, int k) {
  21. int len = getListLength(head);
  22. k = len == ? : len - k%len ;
  23. if(head ==NULL || k== || k==len){
  24. return head;
  25. }
  26. ListNode *newHead = NULL;
  27. ListNode *cur = head;
  28. int cnt = ;
  29. while(cur){
  30. ++cnt;
  31. if(cnt == k){
  32. newHead = cur->next;
  33. cur->next=NULL;
  34. break;
  35. }
  36. cur = cur->next;
  37. }
  38. ListNode* p=newHead;
  39. while(p && p->next){
  40. p = p->next;
  41. }
  42. if(p){
  43. p->next=head;
  44. }
  45. return newHead;
  46. }
  47. };

[Linked List]Rotate List的更多相关文章

  1. [Swift]LeetCode61. 旋转链表 | Rotate List

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

  2. 【LeetCode每天一题】Rotate List(旋转链表)

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

  3. [leetcode]61. Rotate List旋转链表

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

  4. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  5. [LeetCode] 61. Rotate List 旋转链表

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

  6. 力扣 — Rotate List()

    题目描述: 中文: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...

  7. [LC] 61. Rotate List

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

  8. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  9. leetcode 旋转单链表

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

随机推荐

  1. Reverse Linked List II java

    public static ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=head,current=head, ...

  2. 二维数组在text,image的应用

    NSArray *imageArr = @[@[@"查看地图",@"map_hy.png"], @[@"联系号码",@"phone ...

  3. python 基础篇(二)数据类型概述

    正式进入python的学习. 数据类型可以分为身份,类型,数据项三项联合组成. 身份: id() 类型:type() 数据类型:int,boolean,tuple,string,dict,list 1 ...

  4. VirtualBox添加共享文件夹

    直接上图 添加了一个名为"Ubuntu10.04-en"的共享文件夹 但是按照它说的命令 mount -t vboxsf share mount_point 打入,然后悲剧了 错误 ...

  5. Uncaught TypeError: Object [object Object] has no method 'live'

    $( selector ).live( events, data, handler );                // jQuery 1.3+$( document ).delegate( se ...

  6. mac/linux install hg

    MAC OSX 10.9: sudo port -v install mercurial or easy_install mercurial

  7. python之列表、字典的使用

    一.概述:以后你在Linux里面写Python脚本的时候会经常用到Python列表.字典,因为你在以后写脚本的时候,大多数情况下都是对文件进行操作处理,使用字典和列表可以很好的操作文件,得出你想要的结 ...

  8. Python第一天-----简单登录验证

    ----------------------------------------- 编写登录接口 要求:1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 -------------- ...

  9. 翻译题(map使用)

    What Are You Talking About 点我 Problem Description Ignatius is so lucky that he met a Martian yesterd ...

  10. POJ 1631 Bridging signals & 2533 Longest Ordered Subsequence

    两个都是最长上升子序列,所以就放一起了 1631 因为长度为40000,所以要用O(nlogn)的算法,其实就是另用一个数组c来存储当前最长子序列每一位的最小值,然后二分查找当前值在其中的位置:如果当 ...