1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)21:合并兩個(gè)有序鏈表

        共 966字,需瀏覽 2分鐘

         ·

        2020-08-28 13:21

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


        今天和大家聊的問題叫做合并兩個(gè)有序鏈表,我們先來看題面:

        https://leetcode-cn.com/problems/merge-two-sorted-lists/

        Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.

        題意


        將兩個(gè)升序鏈表合并為一個(gè)新的 升序 鏈表并返回。新鏈表是通過拼接給定的兩個(gè)鏈表的所有節(jié)點(diǎn)組成的。

        樣例


        輸入:1->2->4, 1->3->4

        輸出:1->1->2->3->4->4

        題解

        兩個(gè)有序鏈表的排序,實(shí)際上可以看成一個(gè)單鏈表使用歸并排序的最后一個(gè)環(huán)節(jié):“將兩個(gè)排好序的子序列合并為一個(gè)子序列:每次都是從未比較的兩個(gè)子序列的最小值中選出一個(gè)更小值”。

        /**
        ?* Definition for singly-linked list.
        ?* public class ListNode {
        ?* int val;//當(dāng)前節(jié)點(diǎn)的值
        ?* ListNode next;//下一個(gè)節(jié)點(diǎn)的引用值
        ?* ListNode(int x) { val = x; }
        ?* }
        ?*/

        class?Solution?{
        ????public?ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ????????ListNode temp=new?ListNode(0);
        ????????ListNode head=temp;//保留頭節(jié)點(diǎn)的引用
        ????????while(l1!=null&&l2!=null){
        ???????????if(l1.val???????????{
        ???????????????temp.next=l1;
        ???????????????l1=l1.next;
        ???????????}
        ???????????else
        ???????????{
        ???????????????temp.next=l2;
        ???????????????l2=l2.next;
        ???????????}
        ???????????temp=temp.next;
        ????????}
        ????????if(l1==null) temp.next=l2;//l1子序列為空,則直接拼屆l2
        ????????if(l2==null) temp.next=l1;
        ????????return?head.next;//返回頭節(jié)點(diǎn)指向的序列
        ????}
        }

        本題還有其他的解法,沒有一一介紹,大家可以去LeetCode上學(xué)習(xí)一下 。
        好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。

        上期推文:


        LeetCode刷題實(shí)戰(zhàn)1:在數(shù)組上遍歷出花樣

        LeetCode刷題實(shí)戰(zhàn)2:用鏈表模擬加法

        LeetCode刷題實(shí)戰(zhàn)3:最長不重復(fù)子串

        LeetCode刷題實(shí)戰(zhàn)4:兩個(gè)正序數(shù)組的中位數(shù)

        LeetCode刷題實(shí)戰(zhàn)5:判斷回文子串

        LeetCode刷題實(shí)戰(zhàn)6:Z字形變換

        LeetCode刷題實(shí)戰(zhàn)7:整數(shù)反轉(zhuǎn)

        LeetCode刷題實(shí)戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)

        LeetCode刷題實(shí)戰(zhàn)9:求解回文數(shù)

        LeetCode刷題實(shí)戰(zhàn)10:字符串正則匹配

        LeetCode刷題實(shí)戰(zhàn)11: 盛最多水的容器

        LeetCode刷題實(shí)戰(zhàn)12: 整數(shù)轉(zhuǎn)羅馬數(shù)字

        LeetCode刷題實(shí)戰(zhàn)13: 羅馬數(shù)字轉(zhuǎn)整數(shù)

        LeetCode刷題實(shí)戰(zhàn)14: 最長公共前綴

        LeetCode刷題實(shí)戰(zhàn)15:三數(shù)之和

        LeetCode刷題實(shí)戰(zhàn)16: 最接近的三數(shù)之和

        LeetCode刷題實(shí)戰(zhàn)17: 電話號碼的字母組合

        LeetCode刷題實(shí)戰(zhàn)18: 四數(shù)之和

        LeetCode刷題實(shí)戰(zhàn)19:刪除鏈表的倒數(shù)第N個(gè)節(jié)點(diǎn)

        LeetCode刷題實(shí)戰(zhàn)20:有效括號


        瀏覽 58
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            高清无码成人 | 先锋影音av资源网 | 免费人成在线观看视频播放 | 艹艹免费网站 | 99豆花视频 | 三级片国产 | 亚洲777 | 免费黄色视频网站在线观看 | 羞羞偷拍视频 | 妹子超碰 |