Partition List 分割链表
2014-01-19 16:48
197 查看
/**
* 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 dummy1(-1);
ListNode dummy2(-1);
ListNode *prev1=&dummy1;
ListNode *prev2=&dummy2;
ListNode *curr=head;
while(curr!=NULL)
{
if(curr->val<x)
{
prev1->next=curr;
prev1=prev1->next;
}
else
{
prev2->next=curr;
prev2=prev2->next;
}
curr=curr->next;
}
prev1->next=NULL;
prev2->next=NULL;
if(dummy1.next==NULL) return dummy2.next;
else
{
prev1->next=dummy2.next;
return dummy1.next;
}
}
};
* 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 dummy1(-1);
ListNode dummy2(-1);
ListNode *prev1=&dummy1;
ListNode *prev2=&dummy2;
ListNode *curr=head;
while(curr!=NULL)
{
if(curr->val<x)
{
prev1->next=curr;
prev1=prev1->next;
}
else
{
prev2->next=curr;
prev2=prev2->next;
}
curr=curr->next;
}
prev1->next=NULL;
prev2->next=NULL;
if(dummy1.next==NULL) return dummy2.next;
else
{
prev1->next=dummy2.next;
return dummy1.next;
}
}
};
相关文章推荐
- LeetCode-Partition List-分割链表-链表操作
- leetcode 链表 Partition List
- 链表:分割
- 【LeetCode】Partition List ——链表排序问题
- 链表分割(Java)
- [LeetCode] Partition List(!!!!巧妙的链表插入)
- 程序员面试金典2.4:给定值x为基准将链表分割成两部分
- 字符串分割_链表
- LeetCode | Partition List(partition方法处理链表)
- 循环链表(4) - 分割链表为两段
- [Leetcode] Partition list 划分链表
- [LeetCode] Partition List 分割单链表
- [程序员面试金典]-链表分割
- 面试11之以给定值x为基准将链表分割成两部分
- 【LeetCode】Partition List 链表划分 - Easy +
- 《剑指offer》链表分割
- 程序员面试金典(5)以值x分割链表,小的在前面,大于等于的在后面
- 【链表】Partition List
- 【nginx】双向队列(ngx_queue.h)的分割队列(链表) ngx_queue_split操作
- 链表分割