LeetCode-21 Merge Two Sorted Lists Solution (with Java)

1. Description:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-05-07
 3  * Definition for singly-linked list.
 4  * public class ListNode {
 5  *     int val;
 6  *     ListNode next;
 7  *     ListNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
12         ListNode head = new ListNode(-1);
13         ListNode p = l1, q = l2, tail = head;
14         while (p != null && q != null){
15             if(p.val <= q.val){
16                 tail.next  = p;
17                 p = p.next;
18                 tail = tail.next;
19             } else{
20                 tail.next = q;
21                 q = q.next;
22                 tail = tail.next;
23             }
24         }
25         if(p != null)
26             tail.next = p;
27         if(q != null)
28             tail.next = q;
29         return head.next;
30     }
31 }

原文地址:https://www.cnblogs.com/sheepcore/p/12395165.html

时间: 2024-08-01 01:55:38

LeetCode-21 Merge Two Sorted Lists Solution (with Java)的相关文章

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源代

[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. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

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

Leetcode 21. Merge Two Sorted Lists(easy)

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 { * int val; * ListNode *next; * ListNode(i

LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

题目: 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 分析: 将两个有序链表合并为一个新的有序链表并返

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, Li