Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
Posted Lnho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
以上是关于Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]的主要内容,如果未能解决你的问题,请参考以下文章
Leet Code OJ 1. Two Sum [Difficulty: Easy]
Leet Code OJ 223. Rectangle Area [Difficulty: Easy]
Leet Code OJ 338. Counting Bits [Difficulty: Easy]
Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]