【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.

解题分析:

再基础不过的题了,直接看代码吧^-^

具体代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public static ListNode mergeTwoLists(ListNode head1, ListNode head2) {
11         if(head1==null)
12             return head2;
13         if(head2==null)
14             return head1;
15         ListNode head=null;
16         ListNode current=null;
17         if(head1.val<=head2.val){
18             head=head1;
19             head1=head1.next;
20             head.next=null;
21         }
22         else{
23             head=head2;
24             head2=head2.next;
25             head.next=null;
26         }
27         current=head;
28         while(head1!=null&&head2!=null){
29             if(head1.val<=head2.val){
30                 current.next=head1;
31                 current=current.next;
32                 head1=head1.next;
33                 current.next=null;
34             }
35             else{
36                 current.next=head2;
37                 current=current.next;
38                 head2=head2.next;
39                 current.next=null;
40             }
41         }
42         if(head1!=null){
43             current.next=head1;
44         }
45         if(head2!=null){
46             current.next=head2;
47         }
48         return head;
49     }
50 }
时间: 2024-10-17 05:55:23

【leetcode】 21. Merge Two Sorted Lists的相关文章

【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.

【LeetCode】21. Merge Two Sorted Lists合并两个有序链表,得到新的有序链表

一.描述: 二.思路: 两个有着相同排序类型(降序或升序)的链表,合并为一新的链表,并且要求有序(和两个子链表的排序相同): 判断2个子链表是否为空,若有空链表,则返回另一不为空的链表: 两者均不为空,判断链表结点值val的大小,(此处应该有2中排序结果,大->小 或 小->大),该题中提交只接受 小->大 类型: 故新的链表头指针指向val值较小的结点,子链表中去除已经被新链表指向的结点,再次重复该操作,即新链表头指针.next指向下一个较小值对应的结点--,即递归实现. 三.代码:

【LeetCode】23. Merge k Sorted Lists

合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeKLists(vect

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/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 th

C# 写 LeetCode easy #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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

【一天一道LeetCode】#23. Merge k Sorted Lists

一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. (二)解题 合并K个已拍好序的链表.剑指上有合并两个已排好序的链表的算法,那么K个数,我们可以采用归并排序的思想,不过合并函数可能需要修改一下,换成合并两个已排好序的链表的方法.代码如下: /** * Definition for singly-linked

[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】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22