[Leetcode] Partition List
来源:互联网 发布:动态图截取软件 编辑:程序博客网 时间:2024/04/30 23:52
/** * 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* pLess = NULL; ListNode** ppLess = &pLess; ListNode* pGreater = NULL; ListNode** ppGreater = &pGreater; ListNode* cur = head; while (cur) { if (cur->val < x) { *ppLess = cur; ppLess = &(cur->next); } else { *ppGreater = cur; ppGreater = &(cur->next); } cur = cur->next; } *ppLess = NULL; *ppGreater = NULL; if (pLess) { *ppLess = pGreater; return pLess; } else return pGreater; return pLess; }};