[Leetcode] Partition List **

来源:互联网 发布:威廉马歇尔 知乎 编辑:程序博客网 时间:2024/05/11 05:27
/** * 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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        ListNode* pL = NULL;        ListNode* pR = NULL;        ListNode* pLastR = NULL;        ListNode** ppL = &pL;        ListNode** ppR = &pR;                while (head)        {            if (head->val < x)            {                *ppL = head;                ppL = &(head->next);            }            else            {                *ppR = head;                pLastR = head;                ppR = &(head->next);            }            head = head->next;        }                *ppL = pR;        if (pLastR) pLastR->next = NULL;        return pL;    }};

原创粉丝点击