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 ...
随机推荐
- angular路由参数说明
AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...
- 手机端仿ios的三级联动脚本四
二,脚本 <script> $("#city-picker").cityPicker({ title: "选择省市区/县", onChange: f ...
- WPF基础篇之静态资源和动态资源
静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了. 动态资源(DynamicResource)指的是在程序运行过程中然会去访问资源. 一.定义 ...
- 洛谷P3796 【模板】AC自动机(加强版)(AC自动机)
洛谷题目传送门 先膜一发yyb巨佬 orz 想学ac自动机的话,推荐一下yyb巨佬的博客,本蒟蒻也是从那里开始学的. 思路分析 裸的AC自动机,这里就不讲了.主要是这题太卡时了,尽管时限放的很大了.. ...
- 属性动画 ValueAnimator 运行原理全解析
最近下班时间都用来健身还有看书了,博客被晾了一段时间了,原谅我~~~~ 提问环节 好,废话不多说,之前我们已经分析过 View 动画 Animation 运行原理解析,那么这次就来学习下属性动画的运行 ...
- Python基本知识
python是一门编程语言,这是我们学习python首先要了解的内容,那么编程语言又是什么,我们为什么需要编程语言 我们讲的话是汉语,英语或者其他语言,计算机也可以讲话,但是他只会说0,1,也只能看懂 ...
- sql数据库中日期函数---2017-04-12
一.SQLServer时间日期函数详解 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 ...
- 十倍效能提升——Web 基础研发体系的建立
1 导读 web 基础研发体系指的是, web 研发中一线工程师所直接操作的技术.工具,以及所属组织架构的总和.在过去提升企业研发效能的讨论中,围绕的主题基本都是——”通过云计算.云存储等方式将底层核 ...
- Java设计模式(二)抽象工厂模式
一.场景描述 接<Java设计模式(一)工厂模式>https://www.cnblogs.com/mahongbiao/p/8618970.html 工厂模式有一缺点,就是破坏了类的封闭性 ...
- FMDatabaseQueue 如何保证线程安全
这篇文章原来在用 Github Pages 搭建的博客上,现在决定重新用回博客园,所以把文章搬回来. FMDB 是 OC 针对 sqlite 的封装.在其文档的线程安全部分这样讲:同时从多个线程使用同 ...