Leetcode rotate list
来源:互联网 发布:resttemplate json 编辑:程序博客网 时间:2024/06/05 19:38
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode * newhead = NULL;
ListNode * pre=NULL;
ListNode * temp = head;
int i=0;
int length=0;
if(head == NULL)
return NULL;
//find length
while(temp!=NULL)
{
length++;
temp=temp->next;
}
k=k%length;
if (k==0)
return head;
temp=head;
for(i=0; i<length-k;i++)
{
pre=temp;
temp=temp->next;
}
pre->next=NULL;
newhead=temp;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
return newhead;
}
};
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode * newhead = NULL;
ListNode * pre=NULL;
ListNode * temp = head;
int i=0;
int length=0;
if(head == NULL)
return NULL;
//find length
while(temp!=NULL)
{
length++;
temp=temp->next;
}
k=k%length;
if (k==0)
return head;
temp=head;
for(i=0; i<length-k;i++)
{
pre=temp;
temp=temp->next;
}
pre->next=NULL;
newhead=temp;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
return newhead;
}
};
- [leetcode][list] Rotate List
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- [LeetCode]Rotate List
- [leetcode]Rotate List
- LeetCode-Rotate List
- [leetcode] Rotate List
- LeetCode - Rotate List
- LeetCode:Rotate List
- LeetCode 61: Rotate List
- leetcode之Rotate List
- Android下用代码设置静态IP地址的方法(完美支持Android2.X,Android3.X,Android4.X)
- LeetCode Flatten Binary Tree to LinkedList
- linux下重设MYSQL的ROOT密码
- Leetcode sortedArrayToBST
- 整理的oracle实现的基础sql语句查询
- Leetcode rotate list
- 设计模式之装饰模式
- android拦截短信并提取内容
- leetcode String to Integer (atoi)Dec 27 '117626 / 35090
- IGT 2014年校园招聘笔试题
- 大话设计模式二十一:单例模式(Singleton)
- Oracle高可用概述(HA与RAC的关系解惑)
- Leetcode rotate integer
- maven整理