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
.
为了方便,使用了哨兵节点。
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相关推荐
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 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 ...
- LeetCode Rotate Image(矩阵的旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- [LeetCode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode:Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- 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 ...
- leetcode Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [LintCode/LeetCode] Rotate List
Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...
- 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- ...
最新文章
- 2021年普高考成绩查询,山东2021年高考成绩改为6月26日前公布
- oracle中extents存在的理由
- php str_replace多个参数,php str_replace()函数的用法,有那些参数?
- 对象反序列化出现类型不匹配的情况(spring-boot-devtools)
- LeetCode 231. 2的幂 LeetCode 338. 比特位计数(2进制1的个数)
- 用pycharm搭建odoo 12, 11,10 开发调试环境
- Shell脚本:Linux下定时备份MySQL数据库
- SQlite Android数据库的应用 Android SQLite 简易的学生成绩管理系统
- Git可视化工具GitKraKen基本使用
- 无线路由器密码破解-BT3-spoonwep2
- BA--干球温度、露点温度和湿球温度--概念
- flea-db使用之JPA封装介绍
- 珠峰海拔8848米,现在有足够大的纸,厚度是0.01米,折多少次高度可以超过珠穆朗玛峰。(JavaScript)
- 助力金融科技产业发展 360金融AI之夜成功举办
- 量化投资学习——经济周期
- 【感恩】为做运维的重病老同事李静波寻求帮助
- Springboot中使用Shiro的配置与realm的构建
- 文章结构层次序数(序号)的规范要求
- 路径中的“\”和“/”以及相对路径和绝对路径
- 关于Linux的那些事儿--系统状态检测命令
热门文章
- JS设计模式——10.门面模式
- Linux内核套接字(Socket)的设计与实现
- HBase之CMS GC调优
- Spark Streaming之容错机制以及事务语义
- Vivado FIFO IP核接口信号介绍
- 开两个服务内存溢出_应用服务OkHttpClient创建大量对外连接时内存溢出
- dcs 管理计算机的功能,计算机控制系统 第三章 集散控制系统(DCS).doc
- python3默认的字符编码和文件编码_Python的字符编码之三个问题
- 使用stm32 freertos 调试没有问题,正常运行卡死
- 视频封面自动播放两秒钟html,使用videojs轻松搭建一个播放器