prehead
to serve as the starting point of the merged list.prev
and set it to the prehead
.list1
and list2
have nodes to compare:
a. Compare the values of the current nodes of list1
and list2
.
b. If list1
's value is less than or equal to list2
's, attach list1
to prev
and move list1
to its next node.
c. Otherwise, attach list2
to prev
and move list2
to its next node.
d. Move prev
to its next node (the node we just attached).prev
.prehead.next
, which is the head of the merged list.