-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.java
More file actions
48 lines (44 loc) · 1.13 KB
/
Copy pathListNode.java
File metadata and controls
48 lines (44 loc) · 1.13 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
/**
* @Title: TestListNode.java——
* @Package EasyCode_01
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月27日 上午11:26:32
* @version V1.0
*/
package EasyCode_01;
/**
* @ClassName: TestListNode——打印输出链表的工具类 (leetcode上需要使用链表的题目)
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月27日 上午11:26:32
*/
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
public ListNode(int[] arr) {
// TODO Auto-generated constructor stub
if(arr == null || arr.length == 0)
throw new IllegalArgumentException("arr can not be empty");
this.val = arr[0];
ListNode curnode = this;
for (int i = 1; i < arr.length; i++) {
curnode.next = new ListNode(arr[i]);
curnode = curnode.next;
}
}
//返回链表
@Override
public String toString() {
StringBuilder sb = new StringBuilder("");
ListNode curnode = this;
while(curnode != null){
sb.append(Integer.toString(curnode.val));
sb.append("->");
curnode = curnode.next;
}
sb.append("NULL");
return sb.toString();
}
}