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.

为了方便,使用了哨兵节点。

java:

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode rotateRight(ListNode head, int n) {if(head==null||head.next==null||n==0)return head;int cnt=0;ListNode p=head;while(p!=null){cnt++;p=p.next;}if(n==cnt)return head;n=n%cnt;if(0==n)return head;ListNode h = new ListNode(-1);h.next=head;ListNode q=h;int step=0;while(step<n){step++;q=q.next;}p=h;while(q.next!=null){q=q.next;p=p.next;}q.next=h.next;h.next=p.next;p.next=null;return h.next;}
}

c++:

/*** 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||!head->next||0==k){return head;}ListNode *p=head;int N=0;while(p){N++;p=p->next;}if(k==N)return head;k=k%N;if(k==0)return head;ListNode *h = (ListNode *)malloc(sizeof(ListNode));h->next=head;ListNode*q=h;int cnt=0;while(cnt<k){cnt++;q=q->next;}p=h;while(q->next){q=q->next;p=p->next;}q->next=h->next;h->next=p->next;p->next=NULL;ListNode *s =h->next;free(h);return s;}
};

转载于:https://www.cnblogs.com/bingtel/p/4194425.html

leetcode--Rotate List相关推荐

  1. LeetCode——Rotate Image(二维数组顺时针旋转90度)

    问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  2. 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  ...

  3. LeetCode Rotate Image(矩阵的旋转)

     You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  4. [LeetCode] Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  5. LeetCode:Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. [LeetCode]Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

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

  8. leetcode Rotate Image

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  9. [LintCode/LeetCode] Rotate List

    Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...

  10. LeetCode Rotate Function(寻找规律)

    题意:给出一个数组a,长度为n 其中f(0)=0*a[0]+1*a[1]+...+(n-1)*a[n-1] f(1)=0*a[n-1]+1*a[0]+...+(n-1)*a[n-2] ... f(n- ...

最新文章

  1. 2021年普高考成绩查询,山东2021年高考成绩改为6月26日前公布
  2. oracle中extents存在的理由
  3. php str_replace多个参数,php str_replace()函数的用法,有那些参数?
  4. 对象反序列化出现类型不匹配的情况(spring-boot-devtools)
  5. LeetCode 231. 2的幂 LeetCode 338. 比特位计数(2进制1的个数)
  6. 用pycharm搭建odoo 12, 11,10 开发调试环境
  7. Shell脚本:Linux下定时备份MySQL数据库
  8. SQlite Android数据库的应用 Android SQLite 简易的学生成绩管理系统
  9. Git可视化工具GitKraKen基本使用
  10. 无线路由器密码破解-BT3-spoonwep2
  11. BA--干球温度、露点温度和湿球温度--概念
  12. flea-db使用之JPA封装介绍
  13. 珠峰海拔8848米,现在有足够大的纸,厚度是0.01米,折多少次高度可以超过珠穆朗玛峰。(JavaScript)
  14. 助力金融科技产业发展 360金融AI之夜成功举办
  15. 量化投资学习——经济周期
  16. 【感恩】为做运维的重病老同事李静波寻求帮助
  17. Springboot中使用Shiro的配置与realm的构建
  18. 文章结构层次序数(序号)的规范要求
  19. 路径中的“\”和“/”以及相对路径和绝对路径
  20. 关于Linux的那些事儿--系统状态检测命令

热门文章

  1. JS设计模式——10.门面模式
  2. Linux内核套接字(Socket)的设计与实现
  3. HBase之CMS GC调优
  4. Spark Streaming之容错机制以及事务语义
  5. Vivado FIFO IP核接口信号介绍
  6. 开两个服务内存溢出_应用服务OkHttpClient创建大量对外连接时内存溢出
  7. dcs 管理计算机的功能,计算机控制系统 第三章 集散控制系统(DCS).doc
  8. python3默认的字符编码和文件编码_Python的字符编码之三个问题
  9. 使用stm32 freertos 调试没有问题,正常运行卡死
  10. 视频封面自动播放两秒钟html,使用videojs轻松搭建一个播放器