首页 > 技术文章 > Leetcode 86

cunyusup 2018-11-08 17:06 原文

/**
 * 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* pres = new ListNode(-1);
        pres->next = head;
        ListNode* move = pres;
        
        ListNode* preb = new ListNode(-1);
        ListNode* move1 = preb;
        while(move->next != NULL){
            if(move->next->val < x){
                move = move->next;
            }
            else if(move->next->val >= x){
                move1->next = move->next;
                move->next = move->next->next;
                move1->next->next = NULL;
                move1 = move1->next;
            }
        }
        move->next = preb->next;
        return pres->next;
    }
};

 

推荐阅读