File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -24,27 +24,27 @@ class Solution
24
24
ListNode *mergeTwoLists (ListNode *list1, ListNode *list2)
25
25
{
26
26
27
- if (list1 == nullptr )
27
+ if (list1 == nullptr ) // if list1 is empty, return list2
28
28
return list2;
29
- if (list2 == nullptr )
29
+ if (list2 == nullptr ) // if list2 is empty, return list1
30
30
return list1;
31
31
32
- ListNode *head = nullptr ;
33
- ListNode *tail = nullptr ;
32
+ ListNode *head = nullptr ; // head of the merged list
33
+ ListNode *tail = nullptr ; // tail of the merged list
34
34
35
- while (list1 != nullptr && list2 != nullptr )
35
+ while (list1 != nullptr && list2 != nullptr ) //
36
36
{
37
37
if (list1->val < list2->val )
38
38
{
39
39
if (head == nullptr )
40
40
{
41
- head = list1;
42
- tail = list1;
41
+ head = list1; // if head is null, assign list1 to head
42
+ tail = list1; // assign list1 to tail
43
43
}
44
44
else
45
45
{
46
- tail->next = list1;
47
- tail = tail->next ;
46
+ tail->next = list1; // assign list1 to tail->next
47
+ tail = tail->next ; // assign tail->next to tail
48
48
}
49
49
list1 = list1->next ;
50
50
}
You can’t perform that action at this time.
0 commit comments