[LeetCode]89. 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.

 

Subscribe to see which companies asked this question

 
解法1:从头往后,找出第一个值大于等于x的节点,然后在这个节点后面找出第一个值小于x的节点,最后将这个小的节点插在大的节点的前面;然后指针移动到上一步的第一个大节点处,继续循环进行上一步处理,直至后面没有值小于x的节点。
/**
 * 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 || head->next == NULL) return head;
        ListNode *help = new ListNode(0);
        help->next = head;
        ListNode* prev = help, *curr = prev->next;
        while (curr != NULL) {
            while (curr != NULL && curr->val < x)  { // 找出第一个大于等于x的节点
                curr = curr->next;
                prev = prev->next;
            }
            if (curr == NULL) break;
            ListNode *next = curr->next, *temp = curr; // 注意不是从第一个节点开始
            while (next != NULL && next->val >= x) { // 找出第一个小于x的节点
                next = next->next;
                temp = temp->next;
            }
            if (next == NULL) break;
            ListNode* tmp = next->next;
            next->next = prev->next; // 将小于x的节点插在大于等于x的节点之前
            prev->next = next;
            temp->next = tmp;
            prev = next; // 前移到下一个节点
            curr = prev->next;
        }
        return help->next;
    }
};

实际上外层while循环里的第一个while循环可以拿到外层while循环外面,因为后面每次找到的第一个值大于等于x的节点就是curr本身了。

/**
 * 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 || head->next == NULL) return head;
        ListNode *help = new ListNode(0);
        help->next = head;
        ListNode* prev = help;
        while (prev->next != NULL && prev->next->val < x) prev = prev->next;
        ListNode* curr = prev;
        while (curr->next != NULL) {
            if (curr->next != NULL && curr->next->val >= x)
                curr = curr->next;
            else {
                ListNode* tmp = curr->next;
                curr->next = tmp->next;
                tmp->next = prev->next;
                prev->next = tmp;
                prev = prev->next;
            }
        }
        return help->next;
    }
};

 

解法2:另一种方法就是遍历一遍链表,分别将值小于x和值大于等于x的节点分为两个链表链接起来,然后再将后者链接在前者的末尾就可以了。

/**
 * 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* head1 = new ListNode(0);
        ListNode* head2 = new ListNode(0);
        ListNode *next1 = head1, *next2 = head2, *curr = head;
        while (curr != NULL) {
            if (curr->val < x) {
                next1->next = curr;
                next1 = next1->next;
                curr = curr->next;
                next1->next = NULL;
            }
            else {
                next2->next = curr;
                next2 = next2->next;
                curr = curr->next;
                next2->next = NULL;
            }
        }
        next1->next = head2->next;
        return head1->next;
    }
};

 

posted @ 2015-11-16 19:50  AprilCheny  阅读(163)  评论(0编辑  收藏  举报