86_分隔链表

 

package 链表;
/**
 * https://leetcode-cn.com/problems/partition-list/
 * @author Huangyujun
 *
 */
public class _86_分隔链表 {
    public ListNode partition(ListNode head, int x) {
        ListNode small = new ListNode(0);
        ListNode smallHead = small;
        ListNode large = new ListNode(0);
        ListNode largeHead = large;
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            } else {
                large.next = head;
                large = large.next;
            }
            head = head.next;
        }
        large.next = null;
        small.next = largeHead.next;
        return smallHead.next;
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2021-08-21
  • 2021-11-29
  • 2021-07-29
  • 2021-08-18
  • 2021-11-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-11-18
  • 2021-04-17
  • 2021-12-18
相关资源
相似解决方案