首页 > 其他 > 详细

21. 合并两个有序链表

时间:2020-12-24 11:55:57      阅读:30      评论:0      收藏:0      [点我收藏+]

原题链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode re = new ListNode();
        ListNode pvo = re;
        ListNode first = l1;
        ListNode second = l2;
        while (first != null && second != null){
            if (first.val < second.val){
                ListNode temp = new ListNode(first.val);
                first = first.next;
                pvo.next = temp;
            }else{
                ListNode temp = new ListNode(second.val);
                second = second.next;
                pvo.next = temp;
            }
            pvo = pvo.next; 
    }
    if (first != null){
        pvo.next = first;
    }
    if (second != null){
        pvo.next = second;
    }
    return re.next;
    }
}

 

21. 合并两个有序链表

原文:https://www.cnblogs.com/junbaba/p/14182918.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!