Rotate List 旋转链表
2014-01-19 17:06
120 查看
/**
* 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||k==0) return head;
int len=0;
ListNode *p=head;
while(p->next!=NULL)
{
len++;
p=p->next;
}
len++;
k%=len;
int s=len-k;
p->next=head;
for(int i=0;i<s;i++)
{
p=p->next;
}
head=p->next;
p->next=NULL;
return head;
}
};
* 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||k==0) return head;
int len=0;
ListNode *p=head;
while(p->next!=NULL)
{
len++;
p=p->next;
}
len++;
k%=len;
int s=len-k;
p->next=head;
for(int i=0;i<s;i++)
{
p=p->next;
}
head=p->next;
p->next=NULL;
return head;
}
};
相关文章推荐
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- [经典面试题]k节点一组旋转链表
- Python数据结构学习之旋转链表详解
- 面试中常见链表问题3:旋转链表
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- 【链表】Rotate List
- 61.链表旋转
- leetcode——Swap Nodes in Pairs 旋转链表中相邻成对数字(AC)
- 旋转链表
- 数组顺时针旋转90度后输出(行列相等用单向链表实现)
- 乾坤大挪移:链表向右旋转n位
- 练习——旋转链表问题
- leetcode 61. Rotate List 向右旋转k个元素 + 链表环遍历
- 链表翻转、左旋转字符串
- Python 数据结构之旋转链表
- [LeetCode]91. Rotate List旋转链表
- 单链表的建立、输出、倒置旋转
- Leetcode035--使用尾插法局部旋转链表
- 链表的反转与旋转
- 剑指offer 01-06解答思路以及代码(顺序数组找特定数字,替换空格字符,链表反转输出,重建二叉树,两个栈实现队列效果,旋转数组最小元素)