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 两部分,首先想到的是用两个指针从两段向中间扫来求解。但是题目是列表,不能从后往前扫,并且移动后的相对位置要保存和之前一致,则不能用这种方法。

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

     ListNode* partition(ListNode* head, int x) {

         if (head == NULL){
return NULL;
} vector<ListNode*> arr; ListNode* tmp = head;
while (tmp != NULL) { if (tmp->val < x) {
arr.push_back(tmp);
} tmp = tmp->next;
} tmp = head;
while (tmp != NULL) { if (x <= tmp->val) {
arr.push_back(tmp);
} tmp = tmp->next;
} for (int i = ; i < arr.size()-; i++) {
arr[i]->next = arr[i+];
}
arr[arr.size()-]->next = NULL; head = arr[]; return head; }

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

  1. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  4. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

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

  7. LeetCode 86. Partition List 划分链表 C++

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

  8. [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 ...

  9. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

随机推荐

  1. epoll和select区别

    先说下本文框架,先是问题引出,然后概括两个机制的区别和联系,最后介绍每个接口的用法 一.问题引出 联系区别 问题的引出,当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一 ...

  2. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  3. cocos2d-x笔记4: TextField不能删除内容,以及我的解决办法。。。

    3.0正式版,win32下,TextField按下backspace键不能删除内容.网上搜了下,很早就有的问题了,正式版了竟然还不解决... 真心无力吐槽啊!!!这种巨大而又明显的Bug... 从昨天 ...

  4. WebComponent

    WebComponent 前言  最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:& ...

  5. ListToDataTable

    public static DataTable ToDataTable<T>(IEnumerable<T> collection)       {           var ...

  6. 快速发展的Swift是否将淘汰Objective-C?

    随便拉上一个果粉说说这一年来苹果公司的成就,Apple Watch肯定排第一,再下来是iPhone破销量纪录及苹果30亿美元收购Beats Electronics,消息灵通的人说不定还会提到Apple ...

  7. Socket实现简单的聊天通信

    最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...

  8. 【转载】TCP保活(TCP keepalive)

    下图是我遇到tcp keepalive的例子: 以下为转载: TCP保活的缘起 双方建立交互的连接,但是并不是一直存在数据交互,有些连接会在数据交互完毕后,主动释放连接,而有些不会,那么在长时间无数据 ...

  9. JAVA 时间差距,两个时间相差多少天,时,分,秒

    JAVA 时间差距,两个时间相差多少天,时,分,秒 package io; import java.text.DateFormat; import java.text.ParseException; ...

  10. [原博客] POI系列(5)

    正规.严谨.精妙. -POI BZOJ 2213 : [Poi2011]Difference 如果我们每次枚举两个字母最大最小情况时,很容易想到写出代码里注释的样子.这样是26*26*n的,我们发现枚 ...