86. Partition List
List partition 86
2023-09-11 14:22:45 时间
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.
Example:
Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5
AC code:
/** * 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) { ListNode* smallHead = new ListNode(0); ListNode* bigHead = new ListNode(0); ListNode* small = smallHead; ListNode* big = bigHead; while (head != NULL) { ListNode* temp = new ListNode(head->val); if (head->val < x) { small->next = temp; small = small->next; } else { big->next = temp; big = big->next; } head = head->next; } small->next = bigHead->next; return smallHead->next; } };
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Partition List.
这一行代码,写的时候没有注意到:
ListNode* temp = new ListNode(head->val);
相关文章
- Java List合并去重
- (LeetCode 86)Partition List
- [Redux] React Todo List Example (Adding a Todo)
- 【RF库Collections测试】List Should Contain Value
- [Go] golang 两个数组 list 的合并方式
- json串 转 list<class> 方法 List转JSONArray和JSONArray转List String 转List
- Reuse library debug in Chrome - phase3 exchange drop down list
- 【LeetCode】Copy List with Random Pointer
- Java Stream 处理分组后取每组最大&Stream流之list转map、分组取每组第一条&Java 8 Collectors:reducing 示例(List分组取最值)