86分割链表
相似题目链接(题目几乎一模一样):https://leetcode-cn.com/problems/partition-list-lcci/
思路:使用两个指针进行分割比k小的值放入A链表,其他的放入B链表,最后进行连接。
86分割链表 
注意点:
最后一个rTail的next必须进行清空处理,防止后面的next仍然小于K值。像上图的5之后的2值。
public ListNode partition(ListNode head, int x) {
    ListNode headA=new ListNode(0);
    ListNode tailA=headA;
    ListNode headB=new ListNode(0);
    ListNode tailB=headB;
    while (head!=null){
        if(head.val<x){
            tailA.next=head;
            tailA=head;
        }else {
            tailB.next=head;
            tailB=head;
        }
        head=head.next;
    }
    tailB.next=null;
    tailA.next=headB.next;
    return headA.next;
}
 
 

相关文章:

  • 2021-07-29
  • 2021-08-18
  • 2021-11-12
  • 2022-12-23
  • 2021-08-31
  • 2021-05-25
  • 2021-02-22
  • 2021-12-19
猜你喜欢
  • 2022-02-24
  • 2021-11-18
  • 2022-12-23
  • 2021-08-11
  • 2021-08-21
  • 2021-11-29
相关资源
相似解决方案