首页 > 试题广场 >

完善以下代码,以给定x为基准,将链表分割为两部分,所有小于x

[问答题]
完善以下代码,以给定x为基准,将链表分割为两部分,所有小于x的结点排在大于或等于x的结点之前。
public LinkedListNode partition(LinkedListNode node, int x)
{
  LinkedListNode beforeStart = null;
  LinkedListNode afterStart = null;
  
  while( node != null)
  {
LinkedListNode next =   1  ;
if (node.data < x)
{
  node.next =   2   ;
  beforeStart = node;
}
else{
     3  = afterStart;
  afterStart = node;
}
node = next
  }
  if(beforeStart == null){ return afterStart;}

  LinkedListNode head = beforeStart;
  while(beforeStart.next != null){
       4     = beforeStart.next;
  }
  beforeStart.next =   5   ;
  return head;
}
    public ListNode partition(ListNode pHead,int x) {
        //将链表中<x和>x的值分开,但不能改变原来的数据顺序(尾插法)
        //情况:全部小于x,或者全部大于x,有大有小
        //①遍历链表,②把对应节点放到对应区间,③尾插法,④连接两个部分
        if(pHead==null){
            return null;
        }
        ListNode cur=pHead;
        //存入小于x的值
        ListNode bs=null;
        ListNode be=null;
        //存入大于x的值
        ListNode as=null;
        ListNode ae=null;
        //遍历
        while(cur!=null){
            if(cur.val<x){
                //特殊处理第一次插入
                if(bs==null){
                    bs=be=cur;
                }else{
                    be.next=cur;
                    be=be.next;
                }
                //cur=cur.next;
            }else{
                if(as==null){
                    as=ae=cur;
                }else{
                    ae=cur.next;
                    ae=ae.next;
                }
            }
            cur=cur.next;
        }
        //如果前一部分为空
        if(bs==null){
            return as;
        }
        //进行连接
        be.next=as;
        //判断第二部分是否为空
        if(as!=null){
            ae.next=null;
        }
        return bs;
    }

发表于 2024-05-07 21:23:20 回复(0)

public LinkedListNode partition(LinkedListNode node, int x)
{
  LinkedListNode beforeStart = null;
  LinkedListNode afterStart = null;
  
  while( node != null)
  {
LinkedListNode next =  node.next  ;
if (node.data < x)
{
  node.next =   beforeStart   ;
  beforeStart = node;
}
else{
     node.next  = afterStart;
  afterStart = node;
}
node = next
  }
  if(beforeStart == null){ return afterStart;}
  LinkedListNode head = beforeStart;
  while(beforeStart.next != null){
       beforeStart    = beforeStart.next;
  }
  beforeStart.next =   afterStart  ;
  return head;
}
发表于 2022-02-18 10:47:05 回复(0)
public Node partition(int k) {
    Node bStart = null,bEnd = null,aStart = null,aEnd = null;  Node cur = this.head;   while (cur != null) { if (cur.data < k) { if (bStart == null) {
                bStart = cur;  bEnd = cur;  }else {
                bEnd.next = cur;  bEnd = bEnd.next;  }
        }else { if (aStart == null) {
                aStart = cur;  aEnd = cur;  }else {
                aEnd.next = cur;  aEnd = aEnd.next;  }
        }
        cur = cur.next; //自己写的时候忘了    没有这句cur无法往下走  } //拼接  //bStart = null,返回aStart   bStart != null 才需要拼接  //bEnd.next需要为空  if (bStart == null) { return aStart;  }else {
        bEnd.next = aStart;  aEnd.next = null;  } return bStart; }
发表于 2022-02-18 00:16:51 回复(0)