LeetCode刷题笔记(链表):partition-list
2018-01-16 20:55
495 查看
转载请注明作者和出处:http://blog.csdn.net/u011475210
代码地址:https://github.com/WordZzzz/Note/tree/master/LeetCode
刷题平台:https://www.nowcoder.com/ta/leetcode
题 库:Leetcode经典编程题
编 者:WordZzzz
题目描述
解题思路
C版代码实现
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.
具体说来,还是用快慢指针遍历链表,slow指向连续小于x的最后一个元素,fast指向当前元素不小于x但是下个元素小于x的元素。理解清楚这两个指针的对应关系之后,我们很容易将fast指向的元素的下一个元素追加到slow之后,同时更新slow和fast的值。
系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~
完的汪(∪。∪)。。。zzz
代码地址:https://github.com/WordZzzz/Note/tree/master/LeetCode
刷题平台:https://www.nowcoder.com/ta/leetcode
题 库:Leetcode经典编程题
编 者:WordZzzz
题目描述
解题思路
C版代码实现
题目描述
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,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.
解题思路
牛客网上面的答案都是新建两个链表,小于x的放到一个链表里面,不小于的放到另一个链表里面,这种答案感觉好没劲哦。所以最后我采用的是O(n)时间复杂度,O(1)空间复杂度的解法。具体说来,还是用快慢指针遍历链表,slow指向连续小于x的最后一个元素,fast指向当前元素不小于x但是下个元素小于x的元素。理解清楚这两个指针的对应关系之后,我们很容易将fast指向的元素的下一个元素追加到slow之后,同时更新slow和fast的值。
C++版代码实现
/** * 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) { if(head == NULL) return NULL; ListNode *dummy = new ListNode(0); dummy->next = head; ListNode *fast = dummy; ListNode *slow = dummy; while(fast != NULL && fast->next != NULL){ if(fast->next->val >= x) fast = fast->next; else{ if(fast == slow){ fast = fast->next; slow = slow->next; } else{ ListNode *tmp = fast->next; fast->next = tmp->next; tmp->next = slow->next; slow->next = tmp; slow = slow->next; } } } return dummy->next; } };
系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~
完的汪(∪。∪)。。。zzz
相关文章推荐
- LeetCode刷题笔记(链表):remove-duplicates-from-sorted-list-ii
- LeetCode Partition List(链表分段)
- leetcode---partition-list---链表
- leetcode:Partition List (链表处理)【面试算法题】
- LeetCode刷题笔记(链表):sort-list
- Leetcode Partition List 分割链表
- LeetCode刷题笔记(链表):reorder-list
- LeetCode刷题笔记(链表):linked-list-cycle
- LeetCode刷题笔记(链表):linked-list-cycle-ii
- LeetCode刷题笔记(链表):copy-list-with-random-pointer
- LeetCode刷题笔记(链表):rotate-list
- LeetCode刷题笔记(链表):remove-nth-node-from-end-of-list
- LeetCode刷题笔记(链表):convert-sorted-list-to-binary-search-tree
- LeetCode2.2.3 @ Partition List 链表划分 D3F3
- LeetCode刷题笔记(链表):reverse-linked-list-ii
- leetcode_c++:链表:Partition List (086)
- LeetCode(Partition List) 划分链表
- leetcode Partition List链表
- leetcode Partition List二分链表问题
- leetcode:单链表之Partition List