-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode 707
55 lines (51 loc) · 1.14 KB
/
leetcode 707
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
if(index==0){
addAtHead(val);
return;
}
while(p!=null&&i<index-1){
p = p.next;
i++;
}
if(p!=null){
node.next = p.next;
p.next = node;
}
}
public void deleteAtIndex(int index) {
ListNode dummyNode = new ListNode();
dummyNode.next = head;
ListNode p = dummyNode;
int i = 0;
if(index==0){
head = head.next;
}
while(p.next!=null&&i<index){
p = p.next;
i++;
}
if(p.next!=null){
p.next = p.next.next;
}
}
public void show(ListNode list){
ListNode p = list;
while(p!=null){
System.out.print(p.val+" ");
p = p.next;
}
System.out.println();
}
class ListNode {
public int val;
public ListNode next;
public ListNode() {
}
ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}