题目:给定一个链表和一个数x,将链表中比x小的放在前面,其他的放在后头。例如:

Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

思路:

1. 再用两个node,一个指向所有小于x的,一个指向其他的,之后把两个接在一起。接在一起需要注意large是否未移动过。

/**
* 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 || head -> next == NULL) return head;
ListNode *ans_small = new ListNode(); // 存小于x的
ListNode *ans_large = new ListNode(); // 存不小于x的
ans_small -> next = head;
ans_large -> next = head;
ListNode *small = ans_small;
ListNode *large = ans_large;
ListNode *cur = head; while(cur)
{
if (cur -> val < x)
{
small -> next = cur;
small = cur;
cur = cur -> next;
}
else
{
large -> next = cur;
large = cur;
cur = cur -> next;
}
}
large -> next = NULL; // 这个是为了防止large指向head的时候
small -> next = ans_large -> next;
head = ans_small;
delete ans_small, ans_large;
return head -> next;
}
};

2. 就创建一个node,这个node遇见比x小的就插入

class Solution {
public:
ListNode *partition(ListNode *head, int x)
{
if (head == NULL || head -> next == NULL) return head;
ListNode *ans = new ListNode();
ans -> next = head;
ListNode *small = ans; // 在它的后面插入小的
ListNode *cur = head;
ListNode *pre = ans; // cur的前一个指针,方便插入操作
bool flag = true; // true说明要插入的紧接着就是下一个,那就不用插入,加加就好
while(cur != NULL)
{
if (cur -> val < x && small -> next == cur) // 待插入的和之前小的相邻就不用插入了
{
pre = pre -> next;
cur = cur -> next;
small = small -> next;
}
else if (cur -> val < x && small -> next != cur) // 不相邻,插入
{
ListNode *tmpnext = small -> next;
small -> next = cur;
small = cur;
cur = cur -> next;
small -> next = tmpnext;
pre -> next = cur;
flag = true;
}
else if (cur -> val >= x)
{
flag = false;
cur = cur -> next;
pre = pre -> next;
}
}
head = ans;
delete ans;
return head -> next;
}
};

从第二个思路中知道了原来可以判断连个node是否相等!这个之前以为不能那样判断的,原来可以用 if(node1 == node2)。多学一个知识点了。

leetcode[87] Partition List的更多相关文章

  1. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  2. LeetCode 87,远看是字符串其实是搜索,你能做出来吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第54篇文章,我们一起来看LeetCode 87题,Scramble String(爬行字符串). 这题的官方难度 ...

  3. [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  4. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  5. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  6. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

  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 86. Partition List

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

随机推荐

  1. 深入理解Linux修改hostname(转)

    当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...

  2. 公布一个基于 Reactor 模式的 C++ 网络库

    公布一个基于 Reactor 模式的 C++ 网络库 陈硕 (giantchen_AT_gmail) Blog.csdn.net/Solstice 2010 Aug 30 本文主要介绍 muduo 网 ...

  3. iOS6和iOS7适应代码(6) —— NSLocalizedString

    我们的应用程序都需要国际化,字符串的重要组成部分.一般来说.我们是通过一个string资源文件来达到这个目的,我们需要支持多国语言,有多少次把这个文档本地化.需要使用的代码NSLocalizedStr ...

  4. CentOS6.5查看一port执行状态

    netstat -nap | grep 22 版权声明:本文博主原创文章,博客,未经同意不得转载.

  5. httppost body的实现, 和body是gb2312等编码的实现

    使用的是http4.X 版本,里面推荐使用的post是key value的形式 List<NameValuePair> formparams = new ArrayList<Name ...

  6. Android Volley彻底解决(三),定制自己Request

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17612763 经过前面两篇文章的学习,我们已经掌握了Volley各种Request ...

  7. 开源 java CMS - FreeCMS2.3员

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28375.html​ 项目地址:http://www.freeteam.cn/ 会员注冊 打 ...

  8. hdu(2062)-Subset sequence 组合数学

    意甲冠军:查找集合{1,2,3...n}第一m一个排列子. 收集的线索所行的大小. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余 ...

  9. SVN & Git (一)

    (一)SVN的使用.CornerStone图形化管理工具! SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. ...

  10. CSS3+HTML5特效6 - 闪烁的文字

    先看效果 abcd 这个效果也比较简单,利用keyframes对文字的大小.透明度及颜色做循环显示. CSS <style> @-webkit-keyframes flash { 0%{ ...