29_leetcode_Rotate List
来源:互联网 发布:耻辱2盗版优化补丁 编辑:程序博客网 时间:2024/06/18 18:38
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.
1:链表NULL或者k<= 0; 2:统计链表节点个数并k % n; 3:设置两个指针,其中一个指针先走k个节点;4:两个指针同时走,5:注意交换指针
ListNode *rotateRight(ListNode *head, int k) { if(head == NULL || k <= 0) return head; int count = 0; ListNode* index = head; while(index != NULL) { count++; index = index->next; } k = k % count; if( k == 0) return head; ListNode* fast = head; ListNode* slow = head; for(int i = 0; i < k; i++) { fast = fast->next; } while(fast->next) { slow = slow->next; fast = fast->next; } ListNode* tempHead = slow->next; slow->next = NULL; fast->next = head; head = tempHead; return head; }
0 0
- 29_leetcode_Rotate List
- 33_leetcode_Rotate Image
- 29:Partition List
- List
- list
- List
- LIST
- list
- list
- List
- list
- list
- List
- list
- LIST
- List
- List
- List
- Trie树的C++实现
- 28_leetcode_Unique Paths II
- 从数组中找出最大的和最小的数
- ZOJ 2132 The Most Frequent Number
- Struts2防止表单重复提交
- 29_leetcode_Rotate List
- 最大公约数:辗转相除法
- html5游戏开发教程实战:五子棋、四子棋、围棋、翻转棋四种对弈游戏,仅仅100行代码
- 宏函数与普通函数的区别
- SQL Server附加数据库出错:无法打开物理文件,操作系统错误5
- android init 进程分析 (6 辅助模块)
- hadoop基本术语
- 看马云的政治经济学:12亿注资恒大
- java总结文章