LeetCode Partition List
2015-07-01 15:44
429 查看
Description:
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.
Solution:
还是两个指针,一个记录小于x,一个记录大于等于x。
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.
Solution:
还是两个指针,一个记录小于x,一个记录大于等于x。
import java.util.*; public class Solution { public ListNode partition(ListNode head, int x) { ListNode smallerHead = null, biggerHead = null; ListNode smallerTot = smallerHead, biggerTot = biggerHead; ListNode temp = head, neo, next; while (temp != null) { next = temp.next; temp.next = null; if (temp.val < x) { if (smallerHead == null) { smallerHead = temp; smallerTot = temp; } else { smallerTot.next = temp; smallerTot = smallerTot.next; } } else { if (biggerHead == null) { biggerHead = temp; biggerTot = temp; } else { biggerTot.next = temp; biggerTot = biggerTot.next; } } temp = next; } if (smallerHead == null) return biggerHead; else { smallerTot.next = biggerHead; return smallerHead; } } }
相关文章推荐
- Android RelativeLayout和LinearLayout性能分析
- XMLSpy教程
- Dubbo 入门实例 本地伪集群测试Demo (推荐)
- 皕杰报表自定义函数的使用
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
- Android studio java文件显示j爆红
- MSQL优化基础(无直方图选择率)
- CA证书应用一:Outlook发送邮件时,为邮件添加数字签名
- JS实现统计复选框选中个数并提示确定与取消的方法
- oracle 监听hang,监听日志报错:WARNING: Subscription for node down event still pending' in Listener Log
- 面向对象 引用传递
- Redis学习笔记7--Redis管道(pipeline)
- ADB server didn't ACK
- iOS9 new_001:iOS9网络适配(ATS)
- 设计模式之访问者模式——初学
- Mysql主从库同步错误:1062 Error 'Duplicate entry '1438019'
- bootstrap之表格
- CentOS上安装Git
- chapter15test2
- zookeeper单机伪集群安装配置(已验证)