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 ...
随机推荐
- oracle分析函数技术详解(配上开窗函数over())
一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...
- eval及json的理解
一,案例一 <br><br> 复制代码 var str = '{"name": "hanzichi", "age": ...
- jquery绑定onkeyup()事件3中方法
$('input').keyup(function () { ... }); $('input').bind('keyup', function () { ... }); $('input').liv ...
- 关于vue学习中的一些
1.Jinkey大神的新手入门攻略 2.小凡vuejs2的视频地址 3.网友翻译的vuejs2官方中文文档 部分报错处理: 1.错误一 vue.js?b6db:2611[Vue warn]: Unkn ...
- 【Luogu2900】土地征用(斜率优化,动态规划)
[Luogu2900]土地征用(斜率优化,动态规划) 题面 Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块 ...
- 【BZOJ3110】K大数查询(整体二分)
[BZOJ3110]K大数查询(整体二分) 题面 BZOJ 题解 看了很久整体二分 一直不知道哪里写错了 ... 又把树状数组当成线段树区间加法来用了.. 整体二分还是要想清楚在干什么: 我们考虑第\ ...
- 【BZOJ3436】小K的农场(差分约束)
[BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include< ...
- Luogu3118:[USACO15JAN]Moovie Mooving
题面 传送门 Sol 设\(f[S]\)表示看过的电影集合为\(S\),当前电影的最大结束时间 枚举电影和电影的开始时间转移 可以对开始时间\(sort\) 二分一下转移即可 # include &l ...
- html5判断浏览器来源并跳转
<script type="text/javascript"> var system ={}; var p = navigator.platform; system.w ...
- C++输入输出总结_输入
1. 输入输出的本质 C++中的输入输出都是通过流来进行的,而具体的输出输入都是通过对流进行操作来完成的,一般为定向一个流(重定向),清空流,向流里边添加新的元素.C++把输入输出看做字节流,输入时从 ...