lintcode-旋转链表-170
来源:互联网 发布:hadoop python api 编辑:程序博客网 时间:2024/06/02 02:20
给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数
样例
给出链表1->2->3->4->5->null和k=2
返回4->5->1->2->3->null
/** * 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) return NULL; int len=1; ListNode *fast=head; while(fast->next){ ++len; fast=fast->next; } k%=len; fast->next=head; int n=len-k-1; ListNode *cur=head; while(n--) cur=cur->next; head=cur->next; cur->next=NULL; return head; }};
0 0
- lintcode-旋转链表-170
- LintCode 170-旋转链表
- lintcode(170)旋转链表
- [Lintcode] #170 旋转链表
- LintCode-旋转链表
- LintCode-旋转链表
- LintCode:旋转链表
- LintCode : 旋转链表
- 旋转链表-LintCode
- [Lintcode]Rotate List旋转链表
- LintCode-旋转图像
- lintcode-旋转图像-161
- LintCode:旋转图像
- LintCode:旋转字符串
- lintcode ----旋转字符串
- LintCode:旋转图像
- Lintcode 恢复旋转数组
- LintCode 8 旋转字符串
- 单片机5V电源接头的三个引脚
- test5.8
- 用SQL Server实现建表和实现增删改
- 黑、白盒测试基础知识
- 剑指offer--变态跳台阶
- lintcode-旋转链表-170
- Android 高级控件
- Apache Awstats 安装配置系列 (1)
- JAVA中数据类型的划分理解
- MySQL数据库的操作基础
- 机器学习中的优化方法
- gradle--第十一章 使用Gradle命令行3
- C++技术点积累(6)——异常处理、输入和输出流
- MBProgressHUD