-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01965cb
commit 086842d
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode mergeKLists(ListNode[] lists) { | ||
|
||
//ListNode a = new ListNode(); | ||
if(lists.length==0){ | ||
return null; | ||
} | ||
|
||
for(int k=1; k<lists.length; k++) { | ||
lists[k]=mergeTwoLists(lists[k],lists[k-1]); | ||
|
||
|
||
} | ||
return lists[lists.length-1]; | ||
} | ||
|
||
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if(list1 == null){ | ||
return list2; | ||
} | ||
if(list2==null){ | ||
return list1; | ||
} | ||
if(list1.val<list2.val){ | ||
list1.next = mergeTwoLists(list1.next , list2); | ||
return list1; | ||
} | ||
else{ | ||
list2.next = mergeTwoLists(list2.next , list1); | ||
} | ||
return list2; | ||
} | ||
} |