61. Rotate List
来源:互联网 发布:职场色诱 知乎 编辑:程序博客网 时间:2024/06/05 11:46
/*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.思路: 不能忽略k大于链表长度的情况形成环,并对长度取余*//** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* rotateRight(struct ListNode* head, int k){if(!head) return head; struct ListNode *tail=head;struct ListNode *newHead=head;int span=1;while(tail->next){tail=tail->next;span++;}tail->next=head;//形成环if(k%=len){k=len-k;while(k>0){k--;tail=tail->next;}}newHead=tail->next;tail->next=NULL;return newHead;}
阅读全文
0 0
- [LeetCode]61.Rotate List
- 61. Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- MySQL的btree索引和hash索引的区别
- 标准学生类的设计、测试/ / /
- Unix常用指令
- Reflow和Repaint的基本认识和优化
- 二叉树的深度
- 61. Rotate List
- 【亲测】win 10下使用conda安装环境,进行flask web开发
- C++调用Object-C
- 解决Your project contains C++ files but it is not using a supported native build system
- 国家常用代号 for Mysql数据库
- 欢迎使用CSDN-markdown编辑器
- 二分查找的递归和非递归实现
- 如何使用ajax传递字符串数组
- JavaEE软件开发体系架构