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. linq根据传入数据集合查询对应子级数据

    工作中经常用到的linq根据传入数据集合查询对应子级数据,整理共享,希望大家都能用得上,代码中doublesArray 为父节点对应ID数据集合,再根据ID数据集合查询全部子级数据. //获取缓存数据 ...

  2. 小白学爬虫-设置Selenium+Chrome代理

    微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...

  3. Aspose.Words for .NET

    Aspose.Words for .NET Aspose.Words for .NET是 .NET 下先进的 Word 文档处理 API.它支持 DOC, OOXML, RTF, HTML, Open ...

  4. JAVA流式布局管理器--JAVA基础

    JAVA流式布局管理器的使用: FlowLayoutDeme.java: import java.awt.*;import javax.swing.*;public class FlowLayoutD ...

  5. 【BZOJ1997】Planar(2-sat)

    [BZOJ1997]Planar(2-sat) 题面 BZOJ 题解 很久没做过\(2-sat\)了 今天一见,很果断的就来切 这题不难呀 但是有个玄学问题: 平面图的性质:边数\(m\)的最大值为\ ...

  6. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  7. [Luogu3455][POI2007]ZAP-Queries

    BZOJ(权限题) Luogu 题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Securit ...

  8. BZOJ4321: queue2

    题面 传送门 Sol 先设一个套路的状态:\(f[i][j]\)表示到第\(i\)个人,有\(j\)对冲突 但是我们不能确定\(i-1\),所以不好决策i的位置 所以再加一维\(0/1\),\(f[0 ...

  9. PHP 反射类学习记录

    原文:http://www.upwqy.com/details/58.html 1 开发环境 windows TP5 参考文档 http://php.net/manual/zh/class.refle ...

  10. JS中的闭包问题

    一.闭包:在函数外也可使用局部变量的特殊语法现象 全局变量 VS 局部变量: 全局变量:优点:可共享,可重用; 缺点:在任意位置都可随意修改——全局污染 局部变量:优点:安全 缺点:不可共享,不可重用 ...