关于Partition List 的memory limit exceeded问题

public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode greaterHeader = new ListNode(0);
        ListNode lessHeader = new ListNode(0);
        ListNode less = lessHeader;
        ListNode greater = greaterHeader;
        while (head != null) {
            if (head.val < x) {
                less.next = head;
                less = less.next;
                head = head.next;
//                less.next = null;
            } else {
                greater.next = head;
                greater = greater.next;
                head = head.next;
//                greater.next = null;
            }
        }
        less.next = greaterHeader.next;
        return lessHeader.next;
    }
}


想请教,为何将less.next = null; 和greater.next = null; 两处代码去掉,就会产生memory limit exceeded问题。为何一定要叫less.next和greater.next置空才能accept,求高人指点

你可能感兴趣的:(list,partition,limit,memory,exceede)