[LeetCode] 86. Partition List 解题思路

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 and x = 3,
return 1->2->2->4->3->5.

问题:给定一个列表和整数 x,将列表中所有小于 x 的元素移到大于或等于 x 的元素前面。要求,移动后两部分的内部的元素相对位置和原来的保存一致。

一看到题目,需要将一列数分为小于 x 和 大于或等于 x 两部分,首先想到的是用两个指针从两段向中间扫来求解。但是题目是列表,不能从后往前扫,并且移动后的相对位置要保存和之前一致,则不能用这种方法。

第二个思路是用数组存储新的列表顺序,然后在数组中建立 元素间的指针关系。这个思路比较简单,也提交通过了。

 1     ListNode* partition(ListNode* head, int x) {
 2
 3         if (head == NULL){
 4             return NULL;
 5         }
 6
 7         vector<ListNode*> arr;
 8
 9         ListNode* tmp = head;
10         while (tmp != NULL) {
11
12             if (tmp->val < x) {
13                 arr.push_back(tmp);
14             }
15
16             tmp = tmp->next;
17         }
18
19         tmp = head;
20         while (tmp != NULL) {
21
22             if (x <= tmp->val) {
23                 arr.push_back(tmp);
24             }
25
26             tmp = tmp->next;
27         }
28
29         for (int i = 0 ; i < arr.size()-1; i++) {
30             arr[i]->next = arr[i+1];
31         }
32         arr[arr.size()-1]->next = NULL;
33
34         head = arr[0];
35
36         return head;
37
38     }
时间: 2024-10-20 23:14:57

[LeetCode] 86. Partition List 解题思路的相关文章

LeetCode --- 86. Partition List

题目链接:Partition List 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,

[LeetCode] Longest Valid Parentheses 解题思路

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode] 53. Maximum Subarray 解题思路

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 问题: 给定一个元素有正有负的数组,求最大连续子数组的和. 思路

leetCode 86.Partition List(分区链表) 解题思路和方法

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->

【LeetCode】Partition List 解题报告

[题目] 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

[Leetcode] Backtracking回溯法解题思路

碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大.感觉如果要顺利的把题刷下去,必须先要把做的几道题题总结一下. 先放上参考的web: https://segmentfault.com/a/1190000006121957 http://summerisgreen.com/blog/2017-07-07-2017-07-07-算法技巧-backtr

[leetcode]86. Partition List划分链表

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. Input: head = 1->4->3->2->

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右

LeetCode ZigZag Conversion C++ 解题思路

一个难度为Easy的题,看了好多人的题解都没想明白,最后使劲想使劲想,才想的差不多..太弱了,要找不到工作了.. 题目描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A