**题号:86 标题:Partition List 难度:Medium
Given the head of 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.
我的程序码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null){
return head;
}
ListNode temps = new ListNode(); //previous small
ListNode tempeb = new ListNode(); //previous equal big
ListNode small = new ListNode(); // str small
ListNode eqbi = new ListNode(); // str equal big
ListNode Node_index = new ListNode(); //str
ListNode ebst = new ListNode(-101); //head equal big
ListNode sst = new ListNode(-101);//head small
//root = head;
Node_index = head;
int chs=0,cheb=0;
while(Node_index != null){
if(Node_index.val<x){
if(chs == 0 ){
small = Node_index;
sst =small;
temps = sst;
System.out.println("small: "+ Node_index.val);
Node_index = Node_index.next;
chs++;
}else{
temps.next = Node_index;
small = small.next;
temps = small;
System.out.println("small: "+ Node_index.val);
Node_index = Node_index.next;
}
}else{
if(cheb == 0 ){
eqbi = Node_index;
ebst = eqbi;
tempeb = ebst;
cheb++;
System.out.println("eqbi: "+ Node_index.val);
Node_index = Node_index.next;
}else{
tempeb.next = Node_index;
eqbi = eqbi.next;
tempeb = eqbi;
System.out.println("eqbi: "+ Node_index.val);
Node_index = Node_index.next;
}
}
}
eqbi.next = null;
small.next = null;
// while(ebst != null){
// System.out.println("ebst: "+ ebst.val);
// ebst = ebst.next;
// }
// while(sst != null){
// System.out.println("sst: "+ sst.val);
// sst = sst.next;
// }
if(sst.val == -101){
return ebst;
}else if(ebst.val == -101){
return sst;
}else{
temps.next = ebst;
return sst;
}
}
}
DAY19心得
心态炸裂,不想打字
哈罗大家好~ 关於 SharePoint 的应用,到昨天告一段落,回顾一下你可能会觉得,文件库、清单...
为了方便之後丢多点图进行测试,我将图片放进了 img 资料夹,并使用 glob 获得图片列表。 同时...
「曾经沧海难为水,中英不空眼睛痛」,每当看到文字没对齐、段落没缩排或者中文字遇英文字不加空白,就会感...
功能分解对应於各种功能关系,如原始复杂业务功能的开发方式。它主要关注如何开发整体功能及其各个组件之间...
前言:讲解完链结串列的概念後,紧接着就要来进行实作了。 跟做阵列的时候一样,先创建一个新的专案,就可...