题面
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.
审题
linked list 考察链表结构
合并两个有序的链表,使之成为一个新(有序的)表。新表由先前的两个表拼接而成。
code
/**
* 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 headhead=head;
while(true){
if(l1==null&&l2==null){
break;
}
else if((l2!=null)&&(l1==null||l1.val>l2.val)){
headhead.next=l2;
headhead=l2;
l2=l2.next;
}
else{
headhead.next=l1;
headhead=l1;
l1=l1.next;
}
//headhead=headhead.next;
}
return head.next;
}
}