LeetCode Partition List
2014-11-27 17:26
399 查看
链接: https://oj.leetcode.com/problems/partition-list/
双重指针......
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode *partition(ListNode *head,int x)
{
ListNode **t1,**t2;
ListNode *ans1=NULL,*ans2=NULL;
bool s1,s2;
if(head==NULL)
return ans1;
for(s1=true,s2=true;head!=NULL;head=head->next)
{
ListNode *t=NULL;
if(head->val<x)
{
if(s1)
{
t=new ListNode(head->val);
t1=&t;
ans1=t;
s1=false;
}
else
{
*t1=new ListNode(head->val);
}
t1=&((*t1)->next);
}
else
{
if(s2)
{
t=new ListNode(head->val);
t2=&t;
ans2=t;
s2=false;
}
else
{
*t2=new ListNode(head->val);
}
t2=&((*t2)->next);
}
}
if(s1)
return ans2;
if(s2)
return ans1;
*t1=ans2;
return ans1;
}
};
双重指针......
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode *partition(ListNode *head,int x)
{
ListNode **t1,**t2;
ListNode *ans1=NULL,*ans2=NULL;
bool s1,s2;
if(head==NULL)
return ans1;
for(s1=true,s2=true;head!=NULL;head=head->next)
{
ListNode *t=NULL;
if(head->val<x)
{
if(s1)
{
t=new ListNode(head->val);
t1=&t;
ans1=t;
s1=false;
}
else
{
*t1=new ListNode(head->val);
}
t1=&((*t1)->next);
}
else
{
if(s2)
{
t=new ListNode(head->val);
t2=&t;
ans2=t;
s2=false;
}
else
{
*t2=new ListNode(head->val);
}
t2=&((*t2)->next);
}
}
if(s1)
return ans2;
if(s2)
return ans1;
*t1=ans2;
return ans1;
}
};
相关文章推荐
- leetcode 118: Partition List
- LeetCode - Partition List
- LeetCode 086 Partition List
- [C语言][LeetCode][86]Partition List
- Leetcode:Partition List
- leetcode-Partition List
- LeetCode:Partition List
- Leetcode: Partition List
- [LeetCode刷题记录]Partition List
- Leetcode -- Partition List
- 开始刷leetcode day15:Partition List
- LeetCode(87)Partition List
- 【LeetCode】Partition List 链表划分 - Easy +
- LeetCode Partition List
- [Leetcode 86, medium] Partition List
- leetcode笔记--Partition List
- [LeetCode] Partition List
- [LeetCode] Partition List
- leetcode 刷题之路 82 Partition List
- [Leetcode]#86 Partition List