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

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

思路:

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

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *partition(ListNode *head, int x)
  12. {
  13. if (head == NULL || head -> next == NULL) return head;
  14. ListNode *ans_small = new ListNode(); // 存小于x的
  15. ListNode *ans_large = new ListNode(); // 存不小于x的
  16. ans_small -> next = head;
  17. ans_large -> next = head;
  18. ListNode *small = ans_small;
  19. ListNode *large = ans_large;
  20. ListNode *cur = head;
  21.  
  22. while(cur)
  23. {
  24. if (cur -> val < x)
  25. {
  26. small -> next = cur;
  27. small = cur;
  28. cur = cur -> next;
  29. }
  30. else
  31. {
  32. large -> next = cur;
  33. large = cur;
  34. cur = cur -> next;
  35. }
  36. }
  37. large -> next = NULL; // 这个是为了防止large指向head的时候
  38. small -> next = ans_large -> next;
  39. head = ans_small;
  40. delete ans_small, ans_large;
  41. return head -> next;
  42. }
  43. };

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

  1. class Solution {
  2. public:
  3. ListNode *partition(ListNode *head, int x)
  4. {
  5. if (head == NULL || head -> next == NULL) return head;
  6. ListNode *ans = new ListNode();
  7. ans -> next = head;
  8. ListNode *small = ans; // 在它的后面插入小的
  9. ListNode *cur = head;
  10. ListNode *pre = ans; // cur的前一个指针,方便插入操作
  11. bool flag = true; // true说明要插入的紧接着就是下一个,那就不用插入,加加就好
  12. while(cur != NULL)
  13. {
  14. if (cur -> val < x && small -> next == cur) // 待插入的和之前小的相邻就不用插入了
  15. {
  16. pre = pre -> next;
  17. cur = cur -> next;
  18. small = small -> next;
  19. }
  20. else if (cur -> val < x && small -> next != cur) // 不相邻,插入
  21. {
  22. ListNode *tmpnext = small -> next;
  23. small -> next = cur;
  24. small = cur;
  25. cur = cur -> next;
  26. small -> next = tmpnext;
  27. pre -> next = cur;
  28. flag = true;
  29. }
  30. else if (cur -> val >= x)
  31. {
  32. flag = false;
  33. cur = cur -> next;
  34. pre = pre -> next;
  35. }
  36. }
  37. head = ans;
  38. delete ans;
  39. return head -> next;
  40. }
  41. };

从第二个思路中知道了原来可以判断连个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. 用Maven打包成EAR远程部署JBoss(二)——部署到远程JBoss

    用Maven打包成EAR远程部署JBoss(一)讲了如何使用Maven打包,可是在文章的最后也留下了一个问题,那就是如何将包部署到远程的JBoss中呢?近期在对之前的学习进行总结,发现少了这样一篇重要 ...

  2. 华夏互联与逐浪CMS旗下三款软件获著作权登记

     北京时间2014年7月24日消息: 上海逐一软件科技公司长下三款软件通过中国知识产权局登记,当中包含全新的CMS2 x2.0主产品.移动门户管理系统.云台管理系统. 除主产品CMS2 x2.0( ...

  3. The JSON request was too large to be deserialized

    The JSON request was too large to be deserialized 这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理 ...

  4. OC省字典的数组摘要集

    开放式党员 NSString *filePath = @"/Users/dlios/Downloads/area.txt"; 推断错误值 打印出来 NSError *error = ...

  5. 最新Oracle 和 mysql 的对比参照----开发篇(转)

    Oracle mysql 对比版本 Release 10.2.0.1.0 XE  windowsXP 5.0.45-community-nt-log MySQL Community Edition ( ...

  6. Hadoop 2.2.0 HA构造

    在这篇文章中<Ubuntu和CentOS分布式配置Hadoop-2.2.0>介绍hadoop 2.2.0最主要的配置.hadoop 2.2.0中提供了HA的功能,本文在前文的基础上介绍ha ...

  7. android_Activity生命周期功能

    说明:初步activity 生命周期7功能. 样本:于MainActivity我加了button,搬家button.跳到OtherActivity.控制台输出的观察. 让我们来看看这些功能: 他们的流 ...

  8. DirectSound应用

    只是使用的假设PlaySound()这个API函数来显示的声音效果,然后,然后,它不会出现在混合声音,因为PlaySound还有播放期间声音,这将不可避免地导致现有声音停止. 因此,使用 PlaySo ...

  9. .NET中lock的使用方法及注意事项

    lock就是把一段代码定义为临界区,所谓临界区就是同一时刻只能有一个线程来操作临界区的代码,当一个线程位于代码的临界区时,另一个线程不能进入临界区,如果试图进入临界区,则只能一直等待(即被阻止),直到 ...

  10. MVC6项目

    解读ASP.NET 5 & MVC6系列(2):初识项目 2015-05-14 09:08 by 汤姆大叔, 2866 阅读, 19 评论, 收藏, 编辑 初识项目 打开VS2015,创建We ...