forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoLists.java
More file actions
60 lines (50 loc) · 1.49 KB
/
MergeTwoLists.java
File metadata and controls
60 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package array;
import javax.swing.*;
import java.util.LinkedList;
public class MergeTwoLists {
public static void main(String[] args) {
ListNode<Integer> l1 = new ListNode<Integer>(3,new ListNode(4,new ListNode(5,null)));
ListNode<Integer> l2 = new ListNode<Integer>(2,new ListNode(3,new ListNode(4,null)));
ListNode r = mergeTwoLists(l1,l2);
System.out.println(r.toString());
}
private static ListNode mergeTwoLists(ListNode<Integer> l1, ListNode<Integer> l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next,l2);
return l1;
}else{
l2.next = mergeTwoLists(l1,l2.next);
return l2;
}
}
private static class ListNode<T> {
int val;
ListNode next;
ListNode(int x) { val = x; }
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
@Override
public String toString() {
return "ListNode{" +
"val=" + val +
", next=" + next +
'}';
}
}
}