Merge Two Sorted Lists (7.1 Java Edition)

#1

In Merge Two Sorted Lists (7.1 Java Edition) there is a line of code initialising p1 and p2:
ListNode p1=L1, p2 = L2;
My question is why do we need to create variables p1 and p2 at all? Doesnt the code run exactly the same if we just use L1 and L2 instead of p1 and p2?

0 Likes

#2

Hi @programmer99,

You are right. We used to have p1, p2 serving as the iterator variable while traversing through the array. However, like you said, it is not necessary as we can use L1, L2 directly.

1 Like