您的位置:首页 > 其它

leetcode-21Merge Two Sorted Lists

2015-06-25 16:01 344 查看
带头结点

import  java.util.* ;

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 now = head ;
while(l1 != null &&  l2 != null){
if(l1.val <= l2.val){
now.next = new ListNode(l1.val) ;
l1 = l1.next ;
}
else{
now.next = new ListNode(l2.val) ;
l2 = l2.next ;
}
now = now.next ;
}
while(l1 != null){
now.next = new ListNode(l1.val) ;
l1 = l1.next ;
now = now.next ;
}
while(l2 != null){
now.next = new ListNode(l2.val) ;
l2 = l2.next ;
now = now.next ;
}
return  head.next ;
}

public static void main(String[] args){

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: