蒟蒻来水3种思路 💧

合并两个排序的链表

https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

1.思路:直接改变2个链表的内部指针指向,融合为一个大链表;👇

import java.util.*;

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

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @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.思路:将第二个链表的所有元素,都插入到第一个链表中去;(记得用虚拟头结点)👇

   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.思路:新建一个链表,将两个链表的元素整合到新链表,并返回新链表;👇

 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;
        }

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务