-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseList.java
More file actions
79 lines (66 loc) · 1.65 KB
/
ReverseList.java
File metadata and controls
79 lines (66 loc) · 1.65 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
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
package class01;
/**
* @author pacai
* @version 1.0
* 链表逆转
*/
@SuppressWarnings("all")
public class ReverseList {
static class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
}
}
static class DoubleNode<T> {
T data;
DoubleNode<T> next;
DoubleNode<T> pre;
public DoubleNode(T data) {
this.data = data;
}
}
public static Node reverseList(Node head) {
Node<Integer> prev = null;
Node<Integer> next = null;
while (head != null) {
next = next.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
public static DoubleNode reverseListDouble(DoubleNode head) {
DoubleNode<Integer> prev = null;
DoubleNode<Integer> next = null;
while (head != null) {
next = next.next;
head.next = prev;
head.pre = next;
prev = head;
head = next;
}
return prev;
}
public static<T> Node<T> deleteFac(Node<T> head, T num) {
while (head != null) {
if (!head.data.equals(num)) {
break;
}
head = head.next;
}
Node<T> pre = head;
Node<T> cur = head;
while(cur != null){
if(cur.data.equals(num)){
pre.next =cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
return head;
}
}