Leetcode rotate list
来源:互联网 发布:java测试工程师累吗 编辑:程序博客网 时间:2024/05/16 04:28
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
- 4.3 带保护的链栈
- 1063. Set Similarity (25)
- 安装Qt Creator
- 作业六
- 作业5
- Leetcode rotate list
- 属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用?
- 关于 remove 函数
- 作业6
- Leetcode sortedArrayToBST
- 新的 remove 函数
- ubuntu下安装、卸载软件命令
- 安装Qt Creator
- valgrind