Leetcode-86. Partition List
来源:互联网 发布:淘宝一淘入口 编辑:程序博客网 时间:2024/05/23 18:37
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
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
.
public class Solution { public ListNode partition(ListNode head, int x) { if(head == null) return head; if(head.next == null) return head; boolean isExits = false; ListNode headPoint = new ListNode(0); headPoint.next = head; ListNode searchNode = head; ListNode bigNode = null, preNode = headPoint; searchNode = headPoint; while(searchNode != null){ if(searchNode.next != null && searchNode.next.val >= x && bigNode == null) { preNode = searchNode; bigNode = searchNode.next; isExits = true; break; } searchNode = searchNode.next; } if(isExits){ ListNode preSearchNode = preNode; searchNode = bigNode; while(searchNode != null){ if(searchNode.val < x){ preSearchNode.next = searchNode.next; preNode.next = searchNode; searchNode.next = bigNode; preNode = preNode.next; searchNode = preSearchNode.next; }else{ preSearchNode = searchNode; searchNode = searchNode.next; } } } return headPoint.next; }}
0 0
- [LeetCode]86.Partition List
- LeetCode 86. Partition List
- [Leetcode] 86. Partition List
- LeetCode --- 86. Partition List
- [leetcode] 86.Partition List
- [leetcode] 86. Partition List
- [LeetCode]86. Partition List
- 86. Partition List LeetCode
- Leetcode 86. Partition List
- LeetCode *** 86. Partition List
- LeetCode 86. Partition List
- [LeetCode]86. Partition List
- LeetCode 86. Partition List
- leetcode 86. Partition List
- Leetcode 86. Partition List
- LeetCode-86.Partition List
- [Leetcode]86. Partition List
- LeetCode - 86. Partition List
- Ubuntu 14.04LTS 环境下安装UliPad
- 一寸照白底改红底
- JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
- spring入门
- 大公司PHP面试题分享
- Leetcode-86. Partition List
- #欧拉回路
- echo命令
- js常见跨域问题及解决方法
- 【Python】|廖雪峰python 3教程#打印版# #完整版# #吐血整理#
- ajax、jquery调用webservice
- python 多线程编程(一个经典例子)
- 微服务产品级敏捷案例: DJ 企业云存储团队
- ssm集成