欧美1区2区3区激情无套,两个女人互添下身视频在线观看,久久av无码精品人妻系列,久久精品噜噜噜成人,末发育娇小性色xxxx

蒟蒻來(lái)水3種思路 ??

合并兩個(gè)排序的鏈表

http://fangfengwang8.cn/practice/d8b6b4358f774294a89de2a6ac4d9337

1.思路:直接改變2個(gè)鏈表的內(nèi)部指針指向,融合為一個(gè)大鏈表;??

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代碼中的類名、方法名、參數(shù)名已經(jīng)指定,請(qǐng)勿修改,直接返回方法規(guī)定的值即可
     *
     *
     * @param pHead1 ListNode類
     * @param pHead2 ListNode類
     * @return ListNode類
     */
    public ListNode Merge(ListNode pHead1, ListNode pHead2) {
        if (pHead2 == null) return pHead1;
        if (pHead1 == null) return pHead2;
        ListNode ans = pHead1;
        if (pHead1.val > pHead2.val) {
            ans = pHead2;
        }
        ListNode pre1 = pHead1;
        ListNode pre2 = pHead2;
        while (pHead1 != null && pHead2 != null) {
            if (pHead1.val <= pHead2.val) {
                while (pHead1 != null && pHead1.val <= pHead2.val) {
                    pre1 = pHead1;
                    pHead1 = pHead1.next;
                }
                pre1.next = pHead2;
            } else {
                while (pHead2 != null && pHead2.val < pHead1.val) {
                    pre2 = pHead2;
                    pHead2 = pHead2.next;
                }
                pre2.next = pHead1;
            }
        }
        return ans;
    }
}

2.思路:將第二個(gè)鏈表的所有元素,都插入到第一個(gè)鏈表中去;(記得用虛擬頭結(jié)點(diǎn))??

   public ListNode Merge(ListNode pHead1, ListNode pHead2) {
        if (pHead2 == null) return pHead1;
        if (pHead1 == null) return pHead2;
        ListNode dummy = new ListNode(-1);
        dummy.next = pHead1;
        ListNode pre = dummy;
        while (pHead2 != null) {
            while (pHead1 != null && pHead1.val < pHead2.val) {
                pre = pHead1;
                pHead1 = pHead1.next;
            }
            pre.next = new ListNode(pHead2.val);
            pre.next.next = pHead1;
            pre = pre.next;
            pHead2 = pHead2.next;
        }

        return dummy.next;
    }

3.思路:新建一個(gè)鏈表,將兩個(gè)鏈表的元素整合到新鏈表,并返回新鏈表;??

 public ListNode Merge(ListNode pHead1, ListNode pHead2) {
            if(pHead2==null)return pHead1;
            if(pHead1==null)return pHead2;
            ListNode first = pHead1;
            ListNode second = pHead2;
            ListNode ans1 = new ListNode(-1);
            ListNode ans = ans1;
            while (first != null && second != null) {
                if (first.val < second.val) {
                    ans.next = new ListNode((first.val));
                    first = first.next;
                } else {
                    ans.next = new ListNode((second.val));
                    second = second.next;
                }
                ans = ans.next;
            }
            while (second != null) {
                ans.next = new ListNode((second.val));
                second = second.next;
                ans = ans.next;
            }
            while (first != null) {
                ans.next = new ListNode((first.val));
                first = first.next;
                ans = ans.next;
            }
            return ans1.next;
        }

全部評(píng)論

相關(guān)推薦

評(píng)論
1
收藏
分享

創(chuàng)作者周榜

更多
正在熱議
更多
# 面試問(wèn)題記錄 #
60096次瀏覽 873人參與
# 硬件人的簡(jiǎn)歷怎么寫(xiě) #
254176次瀏覽 2884人參與
# 京東TGT #
47327次瀏覽 174人參與
# 你遇到過(guò)哪些神仙同事 #
71558次瀏覽 641人參與
# 我的2024小目標(biāo) #
57935次瀏覽 390人參與
# 工作中,你有沒(méi)有遇到非常愛(ài)罵人的領(lǐng)導(dǎo)? #
16861次瀏覽 126人參與
# 百度工作體驗(yàn) #
204623次瀏覽 1907人參與
# 實(shí)習(xí)生應(yīng)該準(zhǔn)時(shí)下班嗎 #
201429次瀏覽 1314人參與
# 上班到公司第一件事做什么? #
37704次瀏覽 347人參與
# 國(guó)企和大廠硬件兄弟怎么選? #
119965次瀏覽 1656人參與
# 工作一周年分享 #
19021次瀏覽 111人參與
# 沒(méi)有合適的工作,你會(huì)先找個(gè)干著,還是考公考研 #
104226次瀏覽 1075人參與
# 面試吐槽bot #
13945次瀏覽 87人參與
# 互聯(lián)網(wǎng)行業(yè)現(xiàn)在還值得去嗎 #
6232次瀏覽 42人參與
# 面試經(jīng)驗(yàn)談 #
38700次瀏覽 506人參與
# 拼多多工作體驗(yàn) #
16566次瀏覽 152人參與
# 入職第五天,你被拉進(jìn)了幾個(gè)工作群 #
17787次瀏覽 80人參與
# 假如我穿越到了媽媽的18歲 #
6111次瀏覽 43人參與
# 國(guó)企vs私企,你更想去? #
217311次瀏覽 2071人參與
# 你們的畢業(yè)論文什么進(jìn)度了 #
999138次瀏覽 9398人參與
# 機(jī)械人,你的第一份感謝信是誰(shuí)給的 #
26652次瀏覽 296人參與
??途W(wǎng)
??推髽I(yè)服務(wù)