现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
public class Partition {
public ListNode partition(ListNode head, int x) {
// write code here
if (head == null) {
return null;
}
ListNode cur = head;
ListNode bigHead = null;
ListNode bigLast = null;
ListNode smallHead = null;
ListNode smallLast = null;
while (cur != null) {
if (cur.val < x) {
if (smallHead == null) {
smallHead = cur;
smallLast = cur;
} else {
smallLast.next = cur;
smallLast = smallLast.next;
}
} else {
if (bigHead == null) {
bigHead = cur;
bigLast = cur;
} else {
bigLast.next = cur;
bigLast = bigLast.next;
}
}
cur = cur.next;
}
//1、如果前半段没有数据,就直接返回后半段
if (smallHead == null) {
return bigHead;
}
smallLast.next = bigHead;
//2、如果后半段不为空,就将最后一个节点的next置为null
if (bigHead != null) {
bigLast.next = null;
}
return smallHead;
}
} public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null) {
return head;
}
ListNode cur = head;
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
while (cur != null) {
if (cur.val < x) {
if (bs == null) {
bs = cur;
be = cur;
} else {
be.next = cur;
be = be.next;
}
} else {
if (as == null) {
as = cur;
ae = cur;
}else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
if (bs == null) {
return as;
}
be.next = as;
if (as != null) {
ae.next = null;
}
return bs;
} public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
ListNode cur = pHead;
//分成两部分,两部分各自连接好了
while (cur != null) {
if (cur.val < x) {
if (bs == null) { //是第一个元素的情况
bs = cur;
be = cur;
} else {
be.next = cur;
be = cur;
}
} else {
if (as == null) { //是第一个元素的情况
as = cur;
ae = cur;
} else {
ae.next = cur;
ae = cur;
}
}
cur = cur.next;
}
//把这两部分连接起来
//还要考虑这两部分中 有一方不存在或者都不存在的情况
if (bs == null) {
return as;
}
be.next = as;//连接起来的操作
//注意尾巴
if(as != null) {
ae.next = null;
}
return bs;
}
} import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if (pHead == null) {
return null;
}
ListNode cur = pHead;
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
while (cur != null) {
if (cur.val < x) {
if (bs == null) {
bs = cur;
be = cur;
} else {
be.next = cur;
be = be.next;
}
} else {
if (as == null) {
as = cur;
ae = cur;
} else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
//1、bs == null
if (bs == null) {
return as;
}
be.next = as;
if (as != null) {
ae.next = null;
}
return bs;
}
}import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if (pHead == null || pHead.next == null) return pHead;
ListNode bH = null;
ListNode bT = null;
ListNode aH = null;
ListNode aT = null;
ListNode cur = pHead;
ListNode next = null;
while (cur != null) {
next = cur.next;
cur.next = null;
if (cur.val < x) {
if (bH == null) {
bH = cur;
bT = cur;
} else {
bT.next = cur;
bT = bT.next;
}
} else {
if (aH == null) {
aH = cur;
aT = cur;
} else {
aT.next = cur;
aT = aT.next;
}
}
cur = next;
}
//有小于x的节点
if (bT != null) {
bT.next = aH;
}
return bH != null ? bH : aH;
}
}
public class Partition {
public ListNode partition(ListNode pHead, int x) {
if(pHead == null || pHead.next == null){return pHead;};
ListNode newHead = new ListNode(-1);
newHead.next = pHead;
ListNode cur = pHead;
ListNode p = newHead;
ListNode slow = newHead;
while(pHead != null && pHead.val < x ){ //处理头节点就是小于x的情况
pHead = pHead.next; //让最后一个小于x的充作newHead
cur = cur.next;
slow = slow.next;
p = p.next;
}
while(cur != null){
if(cur.val >= x){
cur = cur.next;
slow = slow.next;
}else{
slow.next = cur.next;
cur.next = pHead;
p.next = cur;
p = cur;
cur = slow.next;
}
}
return newHead.next;
}
} import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
/**
思路:首先将整个链表分为2段,第一段的话采用两个引用变量(beforestart,beforeend)
第二段采用两个引用变量(afterstart,afterend)
之所以采用be和ae是因为题目要求要保证原来的数据顺序不能改变,
所以就要使用尾插法,而尾插法就要找到尾巴的位置,用be和ae去保存.
如果没有这两个变量,那么要想找到尾巴还需要再遍历链表,遍历链表就需要时间。
*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
ListNode cur = pHead;
//遍历整个链表,找到比x小的放到第一段,>=x的放到第二段.
//当 cur == null的时候,链表遍历完成.(cur != null的时候说明链表还没遍历完)
while(cur!=null){
if(cur.val <x){
//第一次插入节点
if(bs == null){
bs = cur;
be = cur;
}
//不是第一次插入节点
else {
be.next = cur;
be = be.next;
}
}
else {
if(as == null){
as = cur;
ae = cur;
}
else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
//while循环走完说明链表遍历完成
//要进行判断,如果第一段或者第二段是没有数据的,可能会造成空指针异常
if(bs == null){
return as;
}
if(as != null){
ae.next = null;//如果原链表最后一个元素是小于x的,那么其next就不为null了,那么第二段的最后一个元素的next就需要手动置空,否则会造成死循环,堆溢出,系统找不到链表的尾巴了。
}
be.next = as;
return bs;
}
} public class Partition { public ListNode partition(ListNode pHead, int x) { if(pHead == null) { return null; } ListNode pos = pHead;//用于遍历 ListNode fhead = null;//前半的头 ListNode fcur = null;//前半的连接点 ListNode bhead = null;//后半的头 ListNode bcur = null;//后半的连接点 while(pos != null) { if(pos.val < x) { //第一次插入 - 保持头不动 if(fhead == null) { fhead = pos; fcur = pos;//这里要与fhead关联上 }//不是第一次插入 else { fcur.next = pos; fcur = fcur.next; } }//同上 else { if(bhead == null) { bhead = pos; bcur = pos; } else { bcur.next = pos; bcur = bcur.next; } } pos = pos.next; } //如果全是大于 x 的 if(fhead == null && bhead != null) { bcur.next = null; return bhead; } //如果全是小于 x 的 else if(fhead != null && bhead == null) { //手动制空 fcur = null; }//正常情况 else { fcur.next = bhead; bcur.next = null; } return fhead; } }
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode head, int x) {
// write code here
if(head == null) {
return null;
}
if(head.next == null) {
return head;
}
//定义两个区间段,把比x小的放在bs~be之间,比x大的放在as~ae之间
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
ListNode cur = head;
while(cur != null) {
if(cur.val < x) {
//是不是第一次插入
//默认as ae都是在区间最前面的,然后我们选择让ae动,来标识前半段最后面的节点
if(bs == null) {
bs = cur;
be = cur;
} else {
be.next = cur;
be = be.next;
}
} else {
//同上
if(as == null) {
//第一次插入
as = cur;
ae = cur;
} else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
//如果第一个区间段到最后也没数据,直接返回第二个区间段
if(bs == null) {
return as;
}
be.next = as;//把他们俩连接在一起
if(as != null) {
ae.next = null;
}
return bs;
}
} public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode bs=null;
ListNode be=null;
ListNode as=null;
ListNode ae=null;
ListNode cur=pHead;
while(cur!=null){
if(cur.val<x){
if(bs==null){
bs=cur;
be=cur;
}else{
be.next=cur;
be=cur;
}
}else{
if(as==null){
as=cur;
ae=cur;
}else{
ae.next=cur;
ae=cur;
}
}
cur=cur.next;
}
if(bs==null){
return as;
}
be.next=as;
if(as!=null){
ae.next=null;
}
return bs;
}
} public class Partition {
public ListNode partition(ListNode head, int x) {
//1. 创建五个指针
//创建两个傀儡节点 newhead1 和 newhead2
//再创建这两个傀儡节点的替身,cur1,和 cur2
//创建一个指针 sign 用来遍历原来的链表
ListNode newhead1 = new ListNode(-1);
ListNode newhead2 = new ListNode(-2);
ListNode cur1 = newhead1;
ListNode cur2 = newhead2;
ListNode sign = head;
//2. 进行区间划分
//比 x 值小的放在前一个区间比 x 值大的放在后一个区间
while(sign != null){
if(sign.val < x){
cur1.next = sign;
cur1 = cur1.next;
sign = sign.next;
}else{
cur2.next = sign;
cur2 = cur2.next;
sign = sign.next;
}
}
//3. 傀儡节点失效,重新定义两个区间的头结点
newhead1 = newhead1.next;
newhead2 = newhead2.next;
//4. 连接两个区间
cur1.next = newhead2;
//5. 考虑特殊情况
if(newhead2 != null){
cur2.next = null;
}
if(newhead1 == null ){
return newhead2;
}
return newhead1;
}
} import java.util.*;
public class Partition {
public ListNode partition(ListNode head, int x) {
ListNode bs=null;
ListNode be=null;
ListNode as=null;
ListNode ae=null;
while(head!=null){
if(head.val<x){
if(bs==null){
bs=head;
be=head;
}else{
be.next=head;
be=be.next;
}
head=head.next;
}else{
if(as==null){
as=head;
ae=head;
}else{
ae.next=head;
ae=ae.next;
}
head=head.next;
}
}
if(bs==null){
return as;
}
if(as!=null){
ae.next=null;
}
be.next=as;
return bs;
}
} import java.util.*;
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
//思路:新建两个链表
ListNode less=null;
ListNode more=null;
ListNode moreHead=null;
ListNode lessHead=null;
while (pHead!=null)
{
if(pHead.val<x)
{if(less==null) {
lessHead = new ListNode(pHead.val);
less=lessHead;
}
else {
less.next = new ListNode(pHead.val);
less = less.next;
}
}
else
{if(more==null) {
moreHead= new ListNode(pHead.val);
more=moreHead;
}
else {more.next=new ListNode(pHead.val);
more=more.next;
}}
pHead=pHead.next;
}
if(less==null) {return moreHead;}
less.next=moreHead;
return lessHead;
}
} public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null || pHead.next == null) return pHead;
ListNode newHead = new ListNode(0);
ListNode flagHead = new ListNode(0);
ListNode newh = newHead;
ListNode flagh = flagHead;
while(pHead != null){
if(pHead.val < x){
newh.next = new ListNode(pHead.val);
newh = newh.next;
}else{
flagh.next = new ListNode(pHead.val);
flagh = flagh.next;
}
pHead = pHead.next;
}
newh.next = flagHead.next;
return newHead.next;
}
} public class Partition {
public ListNode partition(ListNode pHead, int x) {
ListNode left , right ,lTop , rTop; // 栈顶与栈底的定义
left = right = lTop = rTop = null;
while (pHead != null) {
if (pHead.val < x) {
if (lTop == null) left = lTop = pHead;
else left = left.next = pHead; // 小的入栈
} else {
if (rTop == null) right = rTop = pHead;
else right = right.next = pHead; // 大的入栈
}
pHead = pHead.next;
}
if (right != null) right.next = null;// 防止成环
if (left != lTop) left.next = rTop; // 小的和大的连接
return left != lTop ? lTop : rTop;
}
} public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null || pHead.next == null){
return pHead;
}
ListNode pCur = pHead;
ListNode lHead = new ListNode(-1);
ListNode hHead = new ListNode(-1);
ListNode lCur = lHead;
ListNode hCur = hHead;
while(pCur != null){
if(pCur.val < x){
lCur.next = new ListNode(pCur.val);
lCur = lCur.next;
}else{
hCur.next = new ListNode(pCur.val);
hCur = hCur.next;
}
pCur = pCur.next;
}
lCur = lHead;
while(lCur.next != null && lHead.next != null){
lCur = lCur.next;
}
if(hHead.next != null){
lCur.next = hHead.next;
}
return lHead.next;
}
}
新建两个头指针,一个负责指向小于x的,一个负责指向大于x的,最后再把他们拼接起来就可以了
class Partition { public: ListNode* partition(ListNode* head, int x) { if(head == nullptr){ return nullptr; }//if ListNode *smallList = new ListNode(-1); ListNode *bigList = new ListNode(-1); ListNode *ps = smallList,*pb = bigList,*cur = head; while(cur){ if(cur->val < x){ ps->next = cur; ps = cur; }//if else{ pb->next = cur; pb = cur; }//else cur = cur->next; }//while pb->next = nullptr; ps->next = bigList->next; return smallList->next; } };