-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
30 lines (28 loc) · 778 Bytes
/
Copy pathListNode.java
File metadata and controls
30 lines (28 loc) · 778 Bytes
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
package com.al.linkedlist;
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
public static ListNode generate(String input){
ListNode cur = null;
ListNode header = null;
String[] val = input.split("->");
for (int i = 0; i < val.length-1; i++) {
if (cur == null) {
cur = new ListNode(Integer.valueOf(val[i]));
header = cur;
} else {
cur.next = new ListNode(Integer.valueOf(val[i]));
cur = cur.next;
}
}
return header;
}
public static void print(ListNode root) {
ListNode cur = root;
while (cur != null) {
System.out.println(cur.val);
cur = cur.next;
}
}
}