Problems:

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

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

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

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

Solution:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
ListNode l3=head;
int n=0;
while(l3!=null)
{
l3=l3.next;
n++;
} l3=head;
int[] a=new int[n];
int[] b=new int[n];
int small=0,big=0;
while(l3!=null)
{
if(l3.val<x)
{
a[small]=l3.val;
small++;
l3=l3.next;
}
else
{
b[big]=l3.val;
big++;
l3=l3.next;
}
} for(int i=0;i<small;i++)
{ if(i==0)
{
l3=new ListNode(a[i]);
head=l3;
}
else
{
l3.next=new ListNode(a[i]);
l3=l3.next;
}
}
for(int i=0;i<big;i++)
{
if(small==0)
{
l3=new ListNode(b[i]);
head=l3;
small=1;
continue;
} l3.next=new ListNode(b[i]);
l3=l3.next; if(i==big-1)
{
l3.next=new ListNode(b[i]);
l3.next=null;
}
}
return head;
}
}

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. Django学习-11-请求相关信息

    requests对象是由类创建的 from django.core.handlers.wsgi import WSGIRequest    --> 类 request.environ中封装了请求 ...

  2. Unity开发之存档和读档的三种实现方式

    此文内容源自siki学院视频,仅供学习!视频链接地址:http://www.sikiedu.com/course/129 工程使用Unity 2017.3.0f3 (64-bit) 老司机读博客,了解 ...

  3. Word巧用大纲视图 快速重排版面

    对于由于内容顺序混乱而造成的目录顺序不当的文章,通常我们一定会想到先对文档内容进行手工排序,然后重新提取目录.但这样操作显然麻烦,而且也容易出错.对于从正文内容自动提取出来的目录,由于按住Ctrl键单 ...

  4. 异常-----freemarker.core.ParseException: Encountered

    1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column 12 in myself.ftl ...

  5. 《你不知道的 JavaScript 上卷》 学习笔记

    第一部分: 作用域和闭包 一.作用域 1. 作用域:存储变量并且查找变量的规则 2. 源代码在执行之前(编译)会经历三个步骤: 分词/此法分析:将代码字符串分解成有意义的代码块(词法单元) 解析/语法 ...

  6. 【BZOJ1007】水平可见直线(单调栈)

    [BZOJ1007]水平可见直线(单调栈) 题解 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的 ...

  7. LightOJ1282 Leading and Trailing

    题面 给定两个数n,k 求n^k的前三位和最后三位 Input Input starts with an integer T (≤ 1000), denoting the number of test ...

  8. c++面试遇到问题

    1. C 和 C++ 区别      2. const 有什么用途     主要有三点: 1:定义只读变量,即常量 2:修饰函数的参数和函数的返回值 3: 修饰函数的定义体,这里的函数为类的成员函数, ...

  9. TP5模型类关键字赋值

    在写项目过程中 开始的代码如下 $ms = new MStore(); $ms->userid = $userid; $ms->address = $mc->address; $ms ...

  10. IT企业如何实现项目管理信息化的目标

    随着信息化技术的不断深入,企业管理方式逐渐向信息化管理转变.大部分IT企业也为了适应企业管理方式的变革,开始加强对管理信息化创新方面的建设.而IT企业在实现信息化的进程中,项目管理信息化其实是IT企业 ...