Rotate List 旋转链表
来源:互联网 发布:人工智能会毁灭人类吗 编辑:程序博客网 时间:2024/05/22 17:41
/**
* 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;
}
};
0 0
- Rotate List 旋转链表
- 旋转链表---Rotate List
- Rotate List 旋转链表 @LeetCode
- LeetCode(Rotate List) 旋转链表
- LeetCode:Rotate List(旋转链表)
- Leetcode Rotate List 旋转链表
- Rotate List(链表旋转)
- Rotate List 部分旋转链表
- [Lintcode]Rotate List旋转链表
- 61. Rotate List (旋转链表)
- LeetCode OJ 之 Rotate List (链表旋转)
- LeetCode 61. Rotate List(旋转链表)
- 170.Rotate List-旋转链表(中等题)
- LeetCode-----61. Rotate List(循环旋转链表)
- Leetcode 61. Rotate List (旋转链表)
- leetcode Rotate list (链表旋转)的python实现
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- leetcode解题61. Rotate List java版(旋转链表)
- wikioi 1069 关押罪犯
- wikioi 1295 N皇后问题
- c++学籍管理系统v1.10
- pku1012
- 正三角图形数列的打印
- Rotate List 旋转链表
- wikioi 1115 开心的金明
- Symmetric Tree
- 祝您在2014新的旅程中身体健康
- wikioi 1166 矩阵取数游戏
- 算法入门之分金币(求极值)
- 软件文档编写入门——软件工程视频总结(二)
- C#中配置文件的操作
- 孟晚舟:华为还没有遇到天花板