Rotate List
来源:互联网 发布:网络风靡的性感骚彤彤 编辑:程序博客网 时间:2024/05/19 14:38
普通的链表问题
/** * 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 == NULL) return head; ListNode *p1 = head; ListNode *p2 = head; ListNode *p3 = head; int count = 0; /*通过循环得到链表的长度*/ while(p1!=NULL) { count++; p2 = p1->next; p1 = p2; } p1 = head; p2 = head; k = k % count; if(k == 0) return head; for(int i = 1;i<(count-k);i++) { p2 = p1->next; p1 = p2; } head = p1->next; p2 = p1->next; p1->next = NULL; p1 = p2; while(p1->next!=NULL) { p2 = p1->next; p1 = p2; } p1->next = p3; return head; }};
0 0
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- rotate-list
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- DataTable填充实体类返回泛型集合
- Java生产者消费者
- static 修饰符
- 2011,回家!
- C&C++点滴
- Rotate List
- 似水往昔浮流年
- ubuntu14.04安装配置jdk7
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
- 安装及配置PHP所遇到的问题汇总
- 一个简单的变长内存池
- Can you solve this equation?
- 第十五周项目2-文件保存不挂科学生信息
- android 4.4 import 工程问题