題目
原題頁面:https://leetcode.com/problems/merge-two-sorted-lists/
題目型別:Linked List
難度評價:Easy
類似題目:(H) Merge k Sorted Lists, (E) Merge Sorted Array, (M) Sort List, (M) Shortest Word Distance II
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
分析
合併兩個有序連結串列,大學期間常見的資料結構練習題。用兩個指標分別指向兩個連結串列頭,然後每次都將兩者中最小的那個節點加入到到合併後的連結串列尾部。注意當其中一個連結串列的節點全部處理完時,別忘了將另一個連結串列剩餘的節點依次全部加入到合併後的連結串列尾部(其實就是將剩餘的子連結串列加到結果連結串列的後面)。
程式碼
1 | class Solution: |
本文地址:https://xnerv.wang/zh-tw/leetcode-merge-two-sorted-lists/
