Update notes

This commit is contained in:
Sainnhe Park 2022-11-14 11:10:19 +08:00
parent eef2af7756
commit d83eb02ae6
2 changed files with 23 additions and 1 deletions

View File

@ -1,4 +1,8 @@
# Summary
# 思路
- [深度优先遍历](./dfs.md)
- [广度优先遍历](./bfs.md)
## 经典代码
- [合并两个有序链表](./merge_two_sorted_linked_lists.md)

View File

@ -0,0 +1,18 @@
# 合并两个有序链表
```cpp
ListNode* mergeTwoLists(ListNode *a, ListNode *b) {
if ((!a) || (!b)) return a ? a : b;
ListNode head, *tail = &head, *aPtr = a, *bPtr = b;
while (aPtr && bPtr) {
if (aPtr->val < bPtr->val) {
tail->next = aPtr; aPtr = aPtr->next;
} else {
tail->next = bPtr; bPtr = bPtr->next;
}
tail = tail->next;
}
tail->next = (aPtr ? aPtr : bPtr);
return head.next;
}
```