/** * 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 if(!head) return NULL; ListNode *lt = new ListNode(0); lt->next = head; ListNode *gt = new ListNode(0); ListNode *p = lt, *q = gt; while(p->next){ if(p->next->val >= x){ q->next = p->next; p->next = q->next->next; q = q->next; q->next = NULL; }else{ p = p->next; } } p->next = gt->next; head = lt->next; delete lt; delete gt; return head; } };
[leetcode]Partition List
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【LeetCode】39. 组合总和
【LeetCode】39. 组合总和
leetcode 数据结构与算法 C++ -
LeetCode: 86. Partition List
x
链表 初始状态 尾插法 -
[LeetCode] 86. Partition List
Given the head of 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 p
leetcode java javascript linked list two pointer