-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathListNode.java
88 lines (68 loc) · 2.09 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.fishercoder.common.classes;
import com.fishercoder.common.utils.CommonUtils;
import java.util.List;
/*
* Normally, both val and next should be private attributes and generate getter and setter for them,
* but for the convenience of leetcode solutions, I set them as public.
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int i) {
this.val = i;
}
public int val() {
return val;
}
public static ListNode createSinglyLinkedList() {
ListNode head = new ListNode(1);
ListNode node1 = new ListNode(2);
head.next = node1;
ListNode node2 = new ListNode(3);
node1.next = node2;
ListNode node3 = new ListNode(4);
node2.next = node3;
ListNode node4 = new ListNode(5);
node3.next = node4;
ListNode node5 = new ListNode(6);
node4.next = node5;
ListNode node6 = new ListNode(7);
node5.next = node6;
ListNode node7 = new ListNode(8);
node6.next = node7;
ListNode node8 = new ListNode(9);
node7.next = node8;
ListNode node9 = new ListNode(10);
node8.next = node9;
return head;
}
public static void main(String... strings) {
List<Integer> values = CommonUtils.randomIntArrayGenerator(10, 20);
ListNode head = createSinglyLinkedList();
System.out.println("The end.");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ListNode)) {
return false;
}
ListNode listNode = (ListNode) o;
if (val != listNode.val) {
return false;
}
return next != null ? next.equals(listNode.next) : listNode.next == null;
}
@Override
public int hashCode() {
int result = val;
result = 31 * result + (next != null ? next.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ListNode{" + "val=" + val + ", next=" + next + '}';
}
}