new ListNode(0); code example

Example 1: new listnode(0) meaning

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    int sizeA = 0, sizeB = 0;
    ListNode ptrA = headA, ptrB = headB;

    while (ptrA != null) {
        ptrA = ptrA.next;
        sizeA++;
    }

    while (ptrB != null) {
        ptrB = ptrB.next;
        sizeB++;
    }

    int diff = sizeA - sizeB;
    if (diff > 0) {
        while (diff-- > 0) {
            headA = headA.next;
        }
    } else {
        while (diff++ < 0) {
            headB = headB.next;
        }
    }

    while (headA != headB) {
        headA = headA.next;
        headB = headB.next;
    }

    return headA;
}

Example 2: new listnode(0) meaning

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}

Tags:

Misc Example