Merge two sorted LinkedLists
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 合并两个XX系列(基础+核心+高频题)
Page 1 of 1
Merge two sorted LinkedLists
public ListNode mergeTwoLists(ListNode listA, ListNode listB){
if(listB == null)
return listA;
if(listA == null)
return listB;
ListNode fakehead = new ListNode(-1);
ListNode ptr = fakehead;
while(listB!=null&&listA!=null){
if(listB.val<listA.val){
ptr.next = listB;
ptr = ptr.next;
listB = listB.next;
}else{
ptr.next = listA;
ptr = ptr.next;
listA = listA.next;
}
}
if(listB!=null)
ptr.next = listB;
if(listA!=null)
ptr.next = listA;
return fakehead.next;
}
if(listB == null)
return listA;
if(listA == null)
return listB;
ListNode fakehead = new ListNode(-1);
ListNode ptr = fakehead;
while(listB!=null&&listA!=null){
if(listB.val<listA.val){
ptr.next = listB;
ptr = ptr.next;
listB = listB.next;
}else{
ptr.next = listA;
ptr = ptr.next;
listA = listA.next;
}
}
if(listB!=null)
ptr.next = listB;
if(listA!=null)
ptr.next = listA;
return fakehead.next;
}
Similar topics
» Merge Two Sorted Array
» Merge Two Sorted List
» Merge K Sorted Array
» Merge K Sorted Arraylist
» Merge K Sorted Linkedlists
» Merge Two Sorted List
» Merge K Sorted Array
» Merge K Sorted Arraylist
» Merge K Sorted Linkedlists
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 合并两个XX系列(基础+核心+高频题)
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|