1
1
package com .diguage .algorithm .leetcode ;
2
2
3
+ import com .diguage .algorithm .util .ListNode ;
4
+
3
5
import java .util .Arrays ;
4
- import java .util .List ;
5
6
import java .util .Objects ;
6
7
8
+ import static com .diguage .algorithm .util .ListNodeUtils .generate ;
9
+ import static com .diguage .algorithm .util .ListNodeUtils .printListNode ;
10
+
7
11
/**
8
12
* = 19. Remove Nth Node From End of List
9
13
*
@@ -64,62 +68,18 @@ public static void main(String[] args) {
64
68
RemoveNthNodeFromEndOfList solution = new RemoveNthNodeFromEndOfList ();
65
69
66
70
ListNode r5 = solution .removeNthFromEnd (generate (Arrays .asList (1 , 2 )), 1 );
67
- print (r5 );
71
+ printListNode (r5 );
68
72
69
73
ListNode r4 = solution .removeNthFromEnd (generate (Arrays .asList (1 )), 1 );
70
- print (r4 );
74
+ printListNode (r4 );
71
75
72
76
ListNode r1 = solution .removeNthFromEnd (generate (Arrays .asList (1 , 2 , 3 , 4 , 5 )), 2 );
73
- print (r1 );
77
+ printListNode (r1 );
74
78
75
79
ListNode r2 = solution .removeNthFromEnd (generate (Arrays .asList (1 , 2 , 3 , 4 , 5 )), 6 );
76
- print (r2 );
80
+ printListNode (r2 );
77
81
78
82
ListNode r3 = solution .removeNthFromEnd (generate (Arrays .asList ()), 2 );
79
- print (r3 );
80
- }
81
-
82
- private static class ListNode {
83
- int val ;
84
- ListNode next ;
85
-
86
- ListNode (int x ) {
87
- val = x ;
88
- }
89
- }
90
-
91
- private static void print (ListNode head ) {
92
- if (Objects .isNull (head )) {
93
- return ;
94
- }
95
- System .out .println ("\n --------" );
96
- ListNode pointer = head ;
97
- while (Objects .nonNull (pointer )) {
98
- int current = pointer .val ;
99
- System .out .print (current + ", " );
100
- pointer = pointer .next ;
101
- }
102
- System .out .println ("\n --------" );
103
- }
104
-
105
- private static ListNode generate (List <Integer > num ) {
106
- if (Objects .isNull (num ) || num .size () == 0 ) {
107
- return null ;
108
- }
109
- ListNode result = null ;
110
- ListNode tail = null ;
111
-
112
- for (int i = 0 ; i < num .size (); i ++) {
113
- int no = num .get (i );
114
- ListNode node = new ListNode (no );
115
- if (Objects .isNull (result )) {
116
- result = node ;
117
- tail = node ;
118
- } else {
119
- tail .next = node ;
120
- tail = node ;
121
- }
122
- }
123
- return result ;
83
+ printListNode (r3 );
124
84
}
125
85
}
0 commit comments