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. Linux之磁盘与文件系统管理

    磁盘及文件系统管理详解 [参考文献:马哥视频] 原文:http://blog.csdn.net/u013008795/article/details/51150075 目前市场上主流的磁盘是机械式硬盘 ...

  2. STM32F4使用FPU+DSP库进行FFT运算的测试过程一

    测试环境:单片机:STM32F407ZGT6   IDE:Keil5.20.0.0  固件库版本:STM32F4xx_DSP_StdPeriph_Lib_V1.4.0 第一部分:使用源码文件的方式,使 ...

  3. 洛谷P4180 [Beijing2010组队]次小生成树Tree(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)

    洛谷题目传送门 %%%TPLY巨佬和ysner巨佬%%% 他们的题解 思路分析 具体思路都在各位巨佬的题解中.这题做法挺多的,我就不对每个都详细讲了,泛泛而谈吧. 大多数算法都要用kruskal把最小 ...

  4. [Luogu3345][ZJOI2015]幻想乡战略游戏

    Luogu 题意: 动态维护带权重心. sol 这是一道写起来很舒服的动态点分治.(不像某些毒瘤题) 我们考虑,如果你选择的补给点不是当前的带权重心,那么带权重心就在补给点的一个子树中(你把补给点当做 ...

  5. [Luogu2057]善意的投票

    题目戳我 题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投 ...

  6. HiHocoder1419 : 后缀数组四·重复旋律4&[SPOJ]REPEATS:Repeats

    题面 Hihocoder Vjudge Sol 题目的提示说的也非常好 我对求\(LCP(P - L + len \% l, P + len \% L)\)做补充 \(len=LCP(P, P + L ...

  7. 宿主机无法访问linux虚机中的网站

    问题现象: Nginx服务已启动 80端口被nginx监听 宿主和linux虚机可相互ping通 Linux虚机可用curl访问网站 宿主无法用浏览器访问网站 排查: 1.  查看nginx的acce ...

  8. Java的类C结构体的实现,以及对类的某一属性进行排序

    public static class Foo { public int x1; public int x2; public int day; } public static Foo[] bridge ...

  9. 【python学习笔记】10.充电时刻

    [python学习笔记]10.充电时刻 任何python都可以作为模块倒入 *.pyc:平台无关的经过编译的的python文件, 模块在第一次导入到程序中时被执行,包括定义类,函数,变量,执行语句 可 ...

  10. View滑动的常见方式

    今天头条的面试官问起了实现View滑动的方式,其实前几天刚刚看过,但还是没能答上来,这里再来总结一遍. 1.使用scrollTo/scrollBy 为了实现View滑动,Android专门提供了这两个 ...