Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: 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.

翻译:

合并2个已经排序的链表,并且返回一个新的链表。这个新的链表应该由前面提到的2个链表的节点所组成。

分析:

注意头节点的处理,和链表结束(next为null)的处理。以下代码新增了一个头指针,来把头节点的处理和普通节点的处理统一了。

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head=new ListNode(0);
        ListNode currentNode=head;
        while(true){
            if(l1==null&&l2==null){
                break;
            }else if(l2!=null&&(l1==null||l1.val>l2.val)){
                currentNode.next=l2;
                l2=l2.next;
            }else{
                currentNode.next=l1;
                l1=l1.next;
            }
            currentNode=currentNode.next;
        }
        return head.next;
    }
}
时间: 2024-10-12 22:05:09

Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

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

Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space comp

Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

Leet Code OJ 58. Length of Last Word [Difficulty: Easy]

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 翻译: 从一个整数链表中移除所有value为val的元素. 例如 给定: 1 –> 2 –> 6 –> 3

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

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

21. Merge Two Sorted Lists(js)

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题意