1
+ package com .crossoverjie .algorithm ;
2
+
3
+ import com .crossoverjie .algorithm .MergeTwoSortedLists .ListNode ;
4
+ import org .junit .Assert ;
5
+ import org .junit .Before ;
6
+ import org .junit .Test ;
7
+
8
+ public class MergeTwoSortedListsTest {
9
+ MergeTwoSortedLists mergeTwoSortedLists ;
10
+ @ Before
11
+ public void setUp () throws Exception {
12
+ mergeTwoSortedLists = new MergeTwoSortedLists ();
13
+ }
14
+
15
+ @ Test
16
+ public void mergeTwoLists () throws Exception {
17
+ ListNode l1 = new ListNode (1 ) ;
18
+ ListNode l1_2 = new ListNode (4 );
19
+ l1 .next = l1_2 ;
20
+ ListNode l1_3 = new ListNode (5 ) ;
21
+ l1_2 .next = l1_3 ;
22
+
23
+ ListNode l2 = new ListNode (1 ) ;
24
+ ListNode l2_2 = new ListNode (3 ) ;
25
+ l2 .next = l2_2 ;
26
+ ListNode l2_3 = new ListNode (6 ) ;
27
+ l2_2 .next = l2_3 ;
28
+ ListNode l2_4 = new ListNode (9 ) ;
29
+ l2_3 .next = l2_4 ;
30
+ ListNode listNode = mergeTwoSortedLists .mergeTwoLists (l1 , l2 );
31
+
32
+
33
+ ListNode node1 = new ListNode (1 ) ;
34
+ ListNode node2 = new ListNode (1 );
35
+ node1 .next = node2 ;
36
+ ListNode node3 = new ListNode (3 ) ;
37
+ node2 .next = node3 ;
38
+ ListNode node4 = new ListNode (4 ) ;
39
+ node3 .next = node4 ;
40
+ ListNode node5 = new ListNode (5 ) ;
41
+ node4 .next = node5 ;
42
+ ListNode node6 = new ListNode (6 ) ;
43
+ node5 .next = node6 ;
44
+ ListNode node7 = new ListNode (9 ) ;
45
+ node6 .next = node7 ;
46
+ Assert .assertEquals (node1 .toString (),listNode .toString ());
47
+
48
+
49
+ }
50
+
51
+ @ Test
52
+ public void mergeTwoLists2 () throws Exception {
53
+
54
+ ListNode l2 = new ListNode (1 ) ;
55
+ ListNode l2_2 = new ListNode (3 ) ;
56
+ l2 .next = l2_2 ;
57
+ ListNode l2_3 = new ListNode (6 ) ;
58
+ l2_2 .next = l2_3 ;
59
+ ListNode l2_4 = new ListNode (9 ) ;
60
+ l2_3 .next = l2_4 ;
61
+ ListNode listNode = mergeTwoSortedLists .mergeTwoLists (null , l2 );
62
+
63
+ System .out .println (listNode .toString ());
64
+
65
+
66
+ }
67
+
68
+ @ Test
69
+ public void mergeTwoLists3 () throws Exception {
70
+
71
+ ListNode l2 = new ListNode (1 ) ;
72
+ ListNode l2_2 = new ListNode (3 ) ;
73
+ l2 .next = l2_2 ;
74
+ ListNode l2_3 = new ListNode (6 ) ;
75
+ l2_2 .next = l2_3 ;
76
+ ListNode l2_4 = new ListNode (9 ) ;
77
+ l2_3 .next = l2_4 ;
78
+ ListNode listNode = mergeTwoSortedLists .mergeTwoLists (l2 , null );
79
+
80
+
81
+ ListNode node1 = new ListNode (1 ) ;
82
+ ListNode node2 = new ListNode (3 );
83
+ node1 .next = node2 ;
84
+ ListNode node3 = new ListNode (6 ) ;
85
+ node2 .next = node3 ;
86
+ ListNode node4 = new ListNode (9 ) ;
87
+ node3 .next = node4 ;
88
+
89
+ Assert .assertEquals (node1 .toString (),listNode .toString ());
90
+
91
+ }
92
+
93
+ }
0 commit comments