码迷,mamicode.com
首页 > 其他好文 > 详细

86.Partition List

时间:2019-04-09 12:26:06      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:lis   val   partition   art   lse   return   next   list   ext   

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        ListNode *pre = dummy, *cur = head;;
        while (pre->next && pre->next->val < x) pre = pre->next;
        cur = pre;
        while (cur->next) {
            if (cur->next->val < x) {
                ListNode *tmp = cur->next;
                cur->next = tmp->next;
                tmp->next = pre->next;
                pre->next = tmp;
                pre = pre->next;
            } else {
                cur = cur->next;
            }
        }
        return dummy->next;
    }
};

86.Partition List

标签:lis   val   partition   art   lse   return   next   list   ext   

原文地址:https://www.cnblogs.com/smallredness/p/10676083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!