首页 > 其他 > 详细

LeetCode86. 分隔链表

时间:2020-12-13 20:37:28      阅读:39      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

☆☆思路:要设置两个虚拟头节点

class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null) return head;
        ListNode smallDummy = new ListNode(0);
        ListNode bigDummy = new ListNode(0);
        ListNode small = smallDummy;
        ListNode big = bigDummy;
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            }else {
                big.next = head;
                big = big.next;
            }
            head = head.next;
        }
        big.next = null;
        small.next = bigDummy.next;
        return smallDummy.next;
    }
}

 

LeetCode86. 分隔链表

原文:https://www.cnblogs.com/HuangYJ/p/14129117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!