Partition List
2014-07-07 06:34
295 查看
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode partition(ListNode head, int x) { ListNode itr = head; ListNode lessDummy = new ListNode(0), greaterEqualDummy = new ListNode(0); ListNode lessItr = lessDummy, greaterEqualItr = greaterEqualDummy; for( ; head != null; head = head.next) { if(head.val < x) { lessItr.next = head; lessItr = head; } else { greaterEqualItr.next = head; greaterEqualItr = head; } } lessItr.next = greaterEqualDummy.next; greaterEqualItr.next = null; return lessDummy.next; } }
Time: O(n)
Space: O(1)
相关文章推荐
- Partition List
- 【LeetCode with Python】 Partition List
- leetcode--Partition List
- [leetcode]Partition List
- Partition List
- LeetCode:Partition List
- LeetCode - Partition List
- Partition List
- 【Leetcode】【Medium】Partition List
- Partition List
- LeetCode--Partition List
- [leetcode] Partition List
- [Linked List]Partition List
- lintcode-easy-Partition List
- leetcode 086 Partition List
- 86. Partition List
- Partition List
- Leetcode Partition List
- [LeetCode] 066: Partition List
- Leetcode算法学习日志-86 Partition List