LeetCode(Rotate List) 旋转链表
来源:互联网 发布:乌班图软件仓库 编辑:程序博客网 时间:2024/06/06 01:43
题目要求:
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
.
代码:
ListNode *rotateRight(ListNode *head, int k) { if(head == NULL || k <=0) return head; int len = ListLength(head); k = k % len; if(k == 0) return head; ListNode* first = head, *second = head; for(size_t i = 0; i < k; ++i) { first = first->next; } while(first->next != NULL) { first = first->next; second = second->next; } ListNode* rotate_head = second->next; second->next = NULL; ListNode* virtual_head = new ListNode(0); virtual_head->next = head; ListNode* cur = virtual_head; while(rotate_head != NULL) { ListNode* node = rotate_head->next; rotate_head->next = cur->next; cur->next = rotate_head; rotate_head = node; cur = cur->next; } ListNode* ret = virtual_head->next; delete virtual_head; virtual_head = NULL; return ret; } int ListLength(ListNode* head) { if(head == NULL) return 0; int len = 0; while(head != NULL) { ++len; head = head->next; } return len; }
0 0
- Rotate List 旋转链表 @LeetCode
- LeetCode(Rotate List) 旋转链表
- LeetCode:Rotate List(旋转链表)
- Leetcode Rotate List 旋转链表
- LeetCode OJ 之 Rotate List (链表旋转)
- LeetCode 61. Rotate List(旋转链表)
- LeetCode-----61. Rotate List(循环旋转链表)
- Leetcode 61. Rotate List (旋转链表)
- leetcode Rotate list (链表旋转)的python实现
- Rotate List 旋转链表
- 旋转链表---Rotate List
- Rotate List(链表旋转)
- Rotate List 部分旋转链表
- [Lintcode]Rotate List旋转链表
- 61. Rotate List (旋转链表)
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- leetcode解题61. Rotate List java版(旋转链表)
- 【LeetCode】 Rotate List 循环链表
- spring配置读取方式给数据迁移带来的风险
- IP冲突导致的ORA-12170
- 从1.5k到18k, 一个程序员的5年成长之路
- 推荐一下PyScripter
- UVa 11177 Fighting Against a Polygonal Monster 圆与多边形公共面积,二分
- LeetCode(Rotate List) 旋转链表
- AppWidgetProvider实现类的配置问题
- MFC menu 事件 dialog 对话框 传递 传值
- (12)html表单<form>之一
- 关于2的补码
- 有一个文件夹/home/a,里面有个模块叫b.py,我怎么把他import到程序里?用import /home/a/a.py不行吧
- 汉明距离
- springmvc2 一个控制器写多个方法(非注解方式)
- 第三章作业