码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Partition List 划分链表

时间:2015-03-08 09:17:20      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

 

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.

 

这道题要求我们划分链表,把所有小于给定值的节点都移到前面,大于该值的节点顺序不变,相当于一个局部排序的问题。那么可以想到的一种解法是首先找到第一个大于或等于给定值的节点,用题目中给的例子来说就是先找到4,然后再找小于3的值,每找到一个就将其取出置于4之前即可,代码如下:

解法一

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if (!head) return head;
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        ListNode *cur = dummy, *pre = dummy, *p = head;
        while (cur->next) {
            if (cur->next->val < x) {
                if (pre == cur) {
                    cur = cur->next;
                    pre = cur;
                    p = cur->next;
                } else {
                    pre->next = cur->next;
                    pre = pre->next;
                    cur->next = cur->next->next;
                    pre->next= p;
                }
            } else {
                cur = cur->next;
            }
        }
        return dummy->next;
    }
};

 

这种解法的链表变化顺序为:

1 -> 4 -> 3 -> 2 -> 5 -> 2 

1 -> 2 -> 4 -> 3 -> 5 -> 2 

1 -> 2 -> 2 -> 4 -> 3 -> 5

 

此题还有一种解法,就是将所有小于给定值的节点取出组成一个新的链表,此时原链表中剩余的节点的值都大于或等于给定值,只要将原链表直接接在新链表后即可,代码如下:

解法二

 

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if (!head) return head;
        ListNode *dummy = new ListNode(-1);
        ListNode *newDummy = new ListNode(-1);
        dummy->next = head;
        ListNode *cur = dummy, *p = newDummy;
        while (cur->next) {
            if (cur->next->val < x) {
                p->next = cur->next;
                p = p->next;
                cur->next = cur->next->next;
                p->next = NULL;
            } else {
                cur = cur->next;
            }
        }
        p->next = dummy->next;
        return newDummy->next;
    }
};

 

此种解法链表变化顺序为:

Original: 1 -> 4 -> 3 -> 2 -> 5 -> 2 

New:

 

Original: 4 -> 3 -> 2 -> 5 -> 2 

New:   1

 

Original: 4 -> 3 -> 5 -> 2 

New:   1 -> 2

 

Original: 4 -> 3 -> 5 

New:   1 -> 2 -> 2

 

Original: 

New:   1 -> 2 -> 2 -> 4 -> 3 -> 5 

 

[LeetCode] Partition List 划分链表

标签:

原文地址:http://www.cnblogs.com/grandyang/p/4321292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!