Java [leetcode 21]Merge Two Sorted Lists

题目描述:

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

解题思路:

题目的意思是将两个有序链表合成一个有序链表。

逐个比较加入到新的链表即可。

代码如下:

public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		ListNode list = new ListNode(0);
		ListNode tmp = list;
		while (l1 != null || l2 != null) {
			if (l1 == null) {
				tmp.next = new ListNode(l2.val);
				l2 = l2.next;
			} else if (l2 == null) {
				tmp.next = new ListNode(l1.val);
				l1 = l1.next;
			} else {
				if (l1.val < l2.val) {
					tmp.next = new ListNode(l1.val);
					l1 = l1.next;
				} else {
					tmp.next = new ListNode(l2.val);
					l2 = l2.next;
				}
			}
			tmp = tmp.next;
		}
		return list.next;
	}
时间: 2024-10-07 01:24:50

Java [leetcode 21]Merge Two Sorted Lists的相关文章

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example Example: Input: 1->2->4, 1-&g

leetCode 21. Merge Two Sorted Lists 合并链表

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)

Problem: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Solution: 两个有序链表,每次取头部最小的那个元素,然后将这个元素从原来链表中去掉,依次循环 题目大意: 给定两个有序链表,要求将链表合并为一个有序链表,并且不能使用额外的空间 Java源代

19.1.29 [LeetCode 21] Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 1 class Solution { 2 public:

LeetCode 21 Merge Two Sorted Lists

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode

LeetCode #21 Merge Two Sorted Lists (E)

[Problem] Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. [Analysis] 没什么好说的,就是按序visit并把两个list的node接在一起. [Solution] public class Solution { public ListNode m

Java [leetcode 23]Merge k Sorted Lists

题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 分而自治方法.将K个List不断地分解为前半部分和后半部分.分别进行两个List的合并.最后将合并的结果合并起来. 代码如下: /** * Definition for singly-linked list. * public class ListNode { * int va

LeetCode 21 Merge Two Sorted Lists 把两个链表有序连接

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 翻译: 把2个有序链表连接,返回一个新链表 思路: 很简单,就是遍历每个节点,小的话添加在新链表的后面.如果一个为空,则把另一个接在新链表结尾. 第二种思路就是以一个链表为基准,然后和另一个比较,如果l2的节