86_分隔链表

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;
    }
}

 

posted @ 2021-12-19 23:05  一乐乐  阅读(19)  评论(0编辑  收藏  举报