-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathListNode.java
68 lines (62 loc) · 1.8 KB
/
ListNode.java
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
61
62
63
64
65
66
67
68
package com.blankj.structure;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/05/18
* desc :
* </pre>
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
/**
* 创建测试数据
*
* @param data [XX,XX,XX]
* @return {@link ListNode}
*/
public static ListNode createTestData(String data) {
if (data.equals("[]")) return null;
data = data.substring(1, data.length() - 1);
String[] split = data.split(",");
int len = split.length;
ListNode[] listNode = new ListNode[len + 1];
listNode[0] = new ListNode(Integer.valueOf(split[0]));
for (int i = 1; i < len; i++) {
listNode[i] = new ListNode(Integer.valueOf(split[i]));
listNode[i - 1].next = listNode[i];
}
return listNode[0];
}
public static void print(ListNode listNode) {
if (listNode == null) {
System.out.println("null");
return;
}
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
ListNode p = listNode.next;
while (p != null) {
str.append(",").append(String.valueOf(p.val));
p = p.next;
}
System.out.println(str.append("]"));
}
public String toString(ListNode listNode) {
if (listNode == null) {
System.out.println("null");
return "";
}
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
ListNode p = listNode.next;
while (p != null) {
str.append(",").append(String.valueOf(p.val));
p = p.next;
}
str.append("]");
return str.toString();
}
}