leetcode_c++:链表:Rotate List(061)
来源:互联网 发布:网络竞速游戏 编辑:程序博客网 时间:2024/05/16 06:11
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.
算法
O(n)
class Solution {public: ListNode* rotateRight(ListNode* head, int k) { if(!head||k<-0) return head; //find the length of the list int len=1; ListNode* p=head; while(p->next!=NULL){ p=p->next; len++; } //connect the tail to head p->next = head; //find the lefy place(k>len) k=len-k%len; //find the place for(int i=0;i<k;i++) p=p->next; //break the list head = p->next; p->next=NULL; return head; }};
0 0
- leetcode_c++:链表:Rotate List(061)
- leetcode_c++:链表:Partition List (086)
- leetcode_c++:链表:Reorder List(143)
- leetcode_c++:链表:Sort List(148)
- leetcode_c++:链表:Reverse Linked List II (092)
- leetcode_c++:链表:Linked List Cycle(141)
- leetcode_c++:链表:Linked List Cycle II(142)
- leetcode_c++:链表:Linked List Cycle II(142)
- leetcode_c++:链表:Insertion Sort List(147)
- leetcode_c++:链表:Remove Linked List Elements(203)
- leetcode_c++:链表:Palindrome Linked List(234)
- leetcode_c++:链表:Odd Even Linked List(328)
- LeetCode 061 Rotate List
- 061 Rotate List [Leetcode]
- LeetCode 061 Rotate List
- 061 - Rotate List
- leetcode 061 Rotate List
- Leetcode 061 Rotate List
- 【字符串】C++字符串编程小结
- ROS学习之 roslaunch
- iOS 判断 当前ViewController是否正在显示
- 快速排序_泰山鲁用村里抢媳妇的故事说明这个算法
- 自定义Toast的显示时长
- leetcode_c++:链表:Rotate List(061)
- hrbust 1584 青蛙过河【二分查找+贪心】
- 算法日记(Java实现)第20160716(2)期——POJ1003/POJ1004
- Java反射Class.forName()配置文件演示
- hibernate学习 hibernate-tutorials(一)——basic
- Android 中this的用法
- photoshop常用快捷键汇总
- 客户端C和服务器S之间建立了一个TCP连接,TCP最大段长度为1KB,客户端C当前的拥塞窗口是16KB,向服务器S连续发送2个最大段之后,成功收到服务器S发送的第一段的确认段,确认段中通告的接受窗口大
- esp8266烧写机智云固件方法