Problems:

  1. 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.
  2.  
  3. You should preserve the original relative order of the nodes in each of the two partitions.
  4.  
  5. For example,
  6. Given 1->4->3->2->5->2 and x = 3,
  7. return 1->2->2->4->3->5

  实现一个patition()方法,里面两个参数,一个链表,一个数字,要求把链表中小于该数字和大于等于该数字的值按照原顺序形成一个新链表。

  其实这是一个比较简单的题目,但是对于从头开始学编程的我用了两次就A掉了,还是比较高兴的,虽然这个程序的效率不是很高,哈哈。

  用两个数组存其中的大小数,然后分别放到链表里面就好了。

Solution:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode partition(ListNode head, int x) {
  11. ListNode l3=head;
  12. int n=0;
  13. while(l3!=null)
  14. {
  15. l3=l3.next;
  16. n++;
  17. }
  18.  
  19. l3=head;
  20. int[] a=new int[n];
  21. int[] b=new int[n];
  22. int small=0,big=0;
  23. while(l3!=null)
  24. {
  25. if(l3.val<x)
  26. {
  27. a[small]=l3.val;
  28. small++;
  29. l3=l3.next;
  30. }
  31. else
  32. {
  33. b[big]=l3.val;
  34. big++;
  35. l3=l3.next;
  36. }
  37. }
  38.  
  39. for(int i=0;i<small;i++)
  40. {
  41.  
  42. if(i==0)
  43. {
  44. l3=new ListNode(a[i]);
  45. head=l3;
  46. }
  47. else
  48. {
  49. l3.next=new ListNode(a[i]);
  50. l3=l3.next;
  51. }
  52. }
  53. for(int i=0;i<big;i++)
  54. {
  55. if(small==0)
  56. {
  57. l3=new ListNode(b[i]);
  58. head=l3;
  59. small=1;
  60. continue;
  61. }
  62.  
  63. l3.next=new ListNode(b[i]);
  64. l3=l3.next;
  65.  
  66. if(i==big-1)
  67. {
  68. l3.next=new ListNode(b[i]);
  69. l3.next=null;
  70. }
  71. }
  72. return head;
  73. }
  74. }

Leetcode 4——Partition List的更多相关文章

  1. LeetCode: Palindrome Partition

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

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

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

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

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

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

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

  5. LeetCode 1043. Partition Array for Maximum Sum

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

  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

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

  8. 【leetcode】Partition List

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

  9. 【leetcode】Partition List(middle)

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

  10. leetcode:Partition List

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

随机推荐

  1. angular路由参数说明

    AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...

  2. 手机端仿ios的三级联动脚本四

    二,脚本 <script> $("#city-picker").cityPicker({ title: "选择省市区/县", onChange: f ...

  3. WPF基础篇之静态资源和动态资源

    静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了. 动态资源(DynamicResource)指的是在程序运行过程中然会去访问资源. 一.定义 ...

  4. 洛谷P3796 【模板】AC自动机(加强版)(AC自动机)

    洛谷题目传送门 先膜一发yyb巨佬 orz 想学ac自动机的话,推荐一下yyb巨佬的博客,本蒟蒻也是从那里开始学的. 思路分析 裸的AC自动机,这里就不讲了.主要是这题太卡时了,尽管时限放的很大了.. ...

  5. 属性动画 ValueAnimator 运行原理全解析

    最近下班时间都用来健身还有看书了,博客被晾了一段时间了,原谅我~~~~ 提问环节 好,废话不多说,之前我们已经分析过 View 动画 Animation 运行原理解析,那么这次就来学习下属性动画的运行 ...

  6. Python基本知识

    python是一门编程语言,这是我们学习python首先要了解的内容,那么编程语言又是什么,我们为什么需要编程语言 我们讲的话是汉语,英语或者其他语言,计算机也可以讲话,但是他只会说0,1,也只能看懂 ...

  7. sql数据库中日期函数---2017-04-12

    一.SQLServer时间日期函数详解 1.  当前系统日期.时间 select getdate() 2. dateadd      在向指定日期加上一段时间的基础上,返回新的 datetime 值 ...

  8. 十倍效能提升——Web 基础研发体系的建立

    1 导读 web 基础研发体系指的是, web 研发中一线工程师所直接操作的技术.工具,以及所属组织架构的总和.在过去提升企业研发效能的讨论中,围绕的主题基本都是——”通过云计算.云存储等方式将底层核 ...

  9. Java设计模式(二)抽象工厂模式

    一.场景描述 接<Java设计模式(一)工厂模式>https://www.cnblogs.com/mahongbiao/p/8618970.html 工厂模式有一缺点,就是破坏了类的封闭性 ...

  10. FMDatabaseQueue 如何保证线程安全

    这篇文章原来在用 Github Pages 搭建的博客上,现在决定重新用回博客园,所以把文章搬回来. FMDB 是 OC 针对 sqlite 的封装.在其文档的线程安全部分这样讲:同时从多个线程使用同 ...