LeetCode【21.合并两个有序连表】

题目描述

将两个有序连表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例

  • 输入:1->2->4, 1->3->4

  • 输出:1->1->2->3->4->4

思路 * 1
初始化一个新的结点,然后使用While循环比较两个链表中的结点的值,再用if,else判断一下,最终哪个链表没有使用完,再接上就可以啦。

代码 * 1

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(-1);
        ListNode cur = res;
        while(l1 !=null && l2 != null) {
            if(l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
                cur = cur.next;
            }else{
                cur.next = l2;
                l2 = l2.next;
                cur = cur.next;
            }
        }
        if(l1 == null )
            cur.next = l2 ;
        else 
            cur.next = l1;
        return res.next;
    }
}

复杂度分析

  • 时间复杂度: O ( N ) O(N) O(N)
  • 空间复杂度: O ( 1 ) O(1) O(1)

思路 * 2
采用递归,把上面的思路用递归的思想写出来就可以了。这个代码量明显比上个小了很多。但不易于使用链表很长的问题
代码 * 2

public ListNode mergeTwoLists(ListNode l1, ListNode l2){
		if(l1 == null) return l2;
		if(l2 == null) return l1;
		if(l1.val < l2.val){
			l1.next = mergeTwoLists(l1.next, l2);
			return l1;
		} else{
			l2.next = mergeTwoLists(l1, l2.next);
			return l2;
		}
}

复杂度分析

  • 时间复杂度: O ( N ) O(N) O(N)
  • 空间复杂度: O ( N ) O(N) O(N)

完整代码

package leetcode21;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * Created by 张超帅 on 2018/9/29.
 */
class ListNode {
    int val;
    ListNode next;
    ListNode (int val) {
        this.val = val;
    }
}
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummpy = new ListNode(-1);
        ListNode cur = dummpy;
        while (l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
                cur = cur.next;
            }else {
                cur.next = l2;
                l2 = l2.next;
                cur = cur.next;
            }
        }
        if(l1 == null) {
            cur.next = l2;
        }else {
            cur.next = l1;
        }
        return dummpy.next;
    }
}
public class leetcode21 {
    public static int[] stringToArrays(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if(input.length() == 0) {
            return new int[0];
        }
        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        int i = 0;
        for (String value : parts) {
            String temp = value.trim();
            output[i ++] = Integer.parseInt(temp);
        }
        return output;
    }
    public static ListNode stringToListNode(String input) {
       int[] nodeValues = stringToArrays(input);
       ListNode dummpy =new ListNode(-1);
       ListNode cur = dummpy;
       for(int value : nodeValues) {
           cur.next = new ListNode(value);
           cur = cur.next;
       }
       return dummpy.next;

    }
    public static String listNodeToString(ListNode head) {
        if (head == null) {
            return "[]";
        }
        String res = "";
        while (head != null) {
            res = res + head.val + ", ";
            head = head.next;
        }
        return "[" + res.substring(0, res.length() - 2) + "]";
    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            ListNode l1 = stringToListNode(line);
            System.out.println(l1.val);
            line = in.readLine();
            ListNode l2 = stringToListNode(line);
            ListNode ret = new Solution().mergeTwoLists(l1, l2);
            System.out.println(ret.val);
            String out = listNodeToString(ret);
            System.out.println(out);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值