Leetcode 4——Partition List
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的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- [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 ...
- 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 ...
- 【leetcode】Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- 【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 ...
- leetcode:Partition List
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes ...
随机推荐
- Carries SCU - 4437
Carries frog has nn integers a1,a2,-,ana1,a2,-,an, and she wants to add them pairwise. Unfortunately ...
- Ball HDU - 4811
Jenny likes balls. He has some balls and he wants to arrange them in a row on the table. Each of tho ...
- HALCON学习-资料
HALCON学习网: http://www.ihalcon.com/ 学习资料推荐博客: http://k594081130.blog.163.com/blog/static/218359013201 ...
- python拓扑排序
发现自己并没有真的理解拓扑排序和多重继承,再次学习了下 拓扑排序要满足如下两个条件 每个顶点出现且只出现一次. 若A在序列中排在B的前面,则在图中不存在从B到A的路径. 拓扑排序算法 任何无回路的顶点 ...
- Dynamics CRM中一个查找字段引发的【血案】
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...
- GridView添加事件监听和常用属性解析
1. 使用流程 graph LR 准备数据源-->新建适配器 新建适配器-->绑定数据源 绑定数据源-->加载适配器 2. 常用属性 android:columnWidth:每一列的 ...
- 简述MyBatis的体系结构
MyBatis体系结构主要由以下几个关键部分: 1.加载配置 配置有两种形式:一种是xml配置文件,另一种是java代码的注解MyBatis将SQL的配置信息加载成为一个个的MappedStateme ...
- webstorm提交版本时,忽略特定文件
项目提交时,部分本地配置文件,不需要提交,这时候需要在整个版本控制中忽略掉文件的提交. 操作如下: File -> Settings -> Version Control -> Ig ...
- 模拟eval
function eval(fn) { var Fn = Function; return new Fn('return' + fn) (); }
- win10每次开机都显示“你的硬件设置已更改,请重启电脑……”的解决办法
之前的系统没有这个问题,就是win10有这个问题,过一段时间就会出现这个问题,网上找了很多,最后发现是显卡驱动的问题,是A卡的问题,只需要更新A卡驱动即可,如果更新A卡驱动不行的话,或者说A卡驱动已经 ...