leetcode:Partition List

来源:互联网 发布:三国志9需要优化哪些 编辑:程序博客网 时间:2024/06/05 12:05
/** * 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 NULL;ListNode *low = new ListNode(-1), *low_tail = low;ListNode *high = new ListNode(-1), *high_tail = high;ListNode *cur = head;while(cur){    if(cur->val<x)    {        low_tail->next = cur;        cur = cur->next;        low_tail = low_tail->next;        low_tail->next = NULL;    }    else    {        high_tail->next = cur;        cur = cur->next;        high_tail = high_tail->next;        high_tail->next = NULL;    }}    low_tail->next = high->next;    return low->next;    }};

0 0