From d83eb02ae65d3b773f4998f164843840c15c0f5d Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Mon, 14 Nov 2022 11:10:19 +0800 Subject: [PATCH] Update notes --- notes/src/SUMMARY.md | 6 +++++- notes/src/merge_two_sorted_linked_lists.md | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 notes/src/merge_two_sorted_linked_lists.md diff --git a/notes/src/SUMMARY.md b/notes/src/SUMMARY.md index b032d2d..095b11b 100644 --- a/notes/src/SUMMARY.md +++ b/notes/src/SUMMARY.md @@ -1,4 +1,8 @@ -# Summary +# 思路 - [深度优先遍历](./dfs.md) - [广度优先遍历](./bfs.md) + +## 经典代码 + +- [合并两个有序链表](./merge_two_sorted_linked_lists.md) diff --git a/notes/src/merge_two_sorted_linked_lists.md b/notes/src/merge_two_sorted_linked_lists.md new file mode 100644 index 0000000..8323fa0 --- /dev/null +++ b/notes/src/merge_two_sorted_linked_lists.md @@ -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; +} +```