Skip to content

Commit df882d5

Browse files
committed
doc: added comments
1 parent 73fd525 commit df882d5

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

cpp/21.merge-two-sorted-lists.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ class Solution
2424
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2)
2525
{
2626

27-
if (list1 == nullptr)
27+
if (list1 == nullptr) // if list1 is empty, return list2
2828
return list2;
29-
if (list2 == nullptr)
29+
if (list2 == nullptr) // if list2 is empty, return list1
3030
return list1;
3131

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
3434

35-
while (list1 != nullptr && list2 != nullptr)
35+
while (list1 != nullptr && list2 != nullptr) //
3636
{
3737
if (list1->val < list2->val)
3838
{
3939
if (head == nullptr)
4040
{
41-
head = list1;
42-
tail = list1;
41+
head = list1; // if head is null, assign list1 to head
42+
tail = list1; // assign list1 to tail
4343
}
4444
else
4545
{
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
4848
}
4949
list1 = list1->next;
5050
}

0 commit comments

Comments
 (0)