Partition List

原创 2015年07月07日 00:22:07

题目描述

Given 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 preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2->null and x = 3,
return 1->2->2->4->3->5->null.

链接地址

http://www.lintcode.com/en/problem/partition-list/

解法

  ListNode *partition(ListNode *head, int x) {
         ListNode *dummyNode1 = new ListNode();
         dummyNode1->next = NULL;
         ListNode *dummyNode2 = new ListNode();
         dummyNode2->next = NULL;
         ListNode *cur = head;
         ListNode *cur1 = dummyNode1;
         ListNode *cur2 = dummyNode2;
         while (cur != NULL) {
             if (cur->val < x) {
                 cur1->next = cur;
                 cur1 = cur1->next;
             }else{
                 cur2->next = cur;
                 cur2 = cur2->next;
             }
             cur = cur->next;
         } 
         cur2->next = NULL;
         cur1->next = dummyNode2->next;
         return dummyNode1->next;
    }

相关文章推荐

C++STL隨手筆記(二)List容器的splice及std::partition用法

list容器內建splice這個函數,可以將容器依指定範圍切割, 以及std的partition可以對容器中的元素進行條件篩選並排續, 稱著記憶猶新趕快記錄下來用法。 #include #in...

leetcode_86_Partition List

思路: 刚开始试着把所有小于x的结点依次插到前面去,但是因为第一个和最后结点的问题真的把我搞得焦头烂额,后来想想,用我媳妇想到的方法可能更清晰一点,用两个链表分别连接小于和大于等于x 的结点,然后再...

leetcode笔记--Partition List

题目:难度(Medium) Given a linked list and a value x, partition it such that all nodes less than x come ...

leetcode || 86、Partition List

problem: Given a linked list and a value x, partition it such that all nodes less than x come ...

Leetcode 86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...

Leetcode Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...

LeetCode Partition List 按值分段链表 系统分析

在原链表操作就是分为三段: 第一段:小值; 第二段:大于等于的值; 第三段:是还没有处理完的值。 但是有3个情况:1 可能开始的时候全部小值, 2 可能开始的时候全部是大值 3 分了三段的情...

【一天一道LeetCode】#86. Partition List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

leetcode[86]:Partition List

Partition ListGiven a linked list and a value x, partition it such that all nodes less than x come b...

Partition List--LeetCode

题目: Given a linked list and a value x, partition it such that all nodes less than x come before n...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Partition List
举报原因:
原因补充:

(最多只允许输入30个字)