LeetCode(87)Partition List

题目如下:

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

分析如下:

我的想法是,遍历链表,生成两个中间结果,small_list和large_list,然后把它们拼接起来成为最终输出。这样编码非常清晰不容易出错。

在网上看到另外一种方法也不错,遍历链表,如果是和x相等或者比x大的节点,就放入链表尾端,如果是比x小的节点,就不做处理,遍历一遍链表之后,就得到了需要的结果。

我的代码:

// 44ms 过大集合
/**
 * 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) {
        if(head==NULL) //特殊情况处理
            return head;
        ListNode* small_head=NULL;
        ListNode* large_head=NULL;
        ListNode* small_tail=NULL;
        ListNode* large_tail=NULL;
        while(head!=NULL){
            if(head->val>=x){
                if(large_head==NULL)
                {
                    large_head=head;
                    large_tail=head;
                }else{
                    large_tail->next=head;
                    large_tail=head;
                }
            }else{
                if(small_head==NULL)
                {
                    small_head=head;
                    small_tail=head;
                }else{
                    small_tail->next=head;
                    small_tail=head;
                }
            }
            head=head->next;
        }
        if(small_head!=NULL){
            small_tail->next=large_head;
             if(large_tail!=NULL)
                large_tail->next=NULL; //末尾增加NULL指针
            return small_head;
        }else{
            if(large_tail!=NULL)
                large_tail->next=NULL; //末尾增加NULL指针
            return large_head;
        }
    }
};

updated: 2014-11-16

发现了一个错误的思路。

1 在构建small_list和large_list的时候,不能加null在表尾。如果加了,相当于给原链表在表尾加了null,就相当于使得原链表断链了。

2 加null是在最后链接small_list和large_list的时候加。此事要注意有4种情况= 2 (small_list存在或者不存在) * 2(large_list存在或者不存在),所以是2个if else嵌套的循环

/**
 * 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* small_head = NULL;
        ListNode* small_tail = NULL;
        ListNode* large_head = NULL;
        ListNode* large_tail = NULL;
        ListNode* index = head;
        while(index != NULL) {
            if (index->val < x) {
                if (small_head == NULL){
                    small_head = index;
                    small_tail = index;
                    //small_tail->next = NULL;//这里不能加NULL,否则相当于对原链表断表了。
                }else {
                    small_tail->next = index;
                    small_tail = small_tail->next;
                    //small_tail->next = NULL;//这里不能加NULL,否则相当于对原链表断表了。
                }
            } else {
                if (large_head == NULL){
                    large_head = index;
                    large_tail = index;
                    //large_tail->next = NULL; //这里不能加NULL,否则相当于对原链表断表了。
                }else {
                    large_tail->next = index;
                    large_tail = large_tail->next;
                    //large_tail->next = NULL;//这里不能加NULL,否则相当于对原链表断表了。
                }
            }
            index = index->next;
        }
        if (small_head != NULL) {
            small_tail->next = large_head;
            if (large_tail != NULL)  //2层if else嵌套,4种情况
                large_tail->next = NULL;
            return small_head;
        } else {
            if (large_tail != NULL)
                large_tail->next = NULL;
            return large_head;
        }
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值