forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT18.java
More file actions
99 lines (91 loc) · 3.25 KB
/
Copy pathT18.java
File metadata and controls
99 lines (91 loc) · 3.25 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package books;
/**
* @program JavaBooks
* @description: 删除链表的节点 & 删除链表中重复的节点
* @author: mf
* @create: 2019/08/29 14:49
*/
/*
在O(1)时间内删除链表节点
给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点
链表节点与函数的定义如下
*/
public class T18 {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
System.out.println("source ListNode...");
printListNode(listNode1);
deleteNode(listNode1, listNode3);
System.out.println("deleted ListNode...");
printListNode(listNode1);
// ListNode listNode1 = new ListNode(1);
// ListNode listNode2 = new ListNode(2);
// ListNode listNode3 = new ListNode(2);
// ListNode listNode4 = new ListNode(3);
//
// listNode1.next = listNode2;
// listNode2.next = listNode3;
// listNode3.next = listNode4;
// System.out.println("source");
// printListNode(listNode1);
// deleteDuplication(listNode1);
// System.out.println("deleted");
// printListNode(listNode1);
}
private static void deleteNode(ListNode headListNode, ListNode pListNode) {
// 要删除的节点不是尾节点
if (pListNode.next != null) {
ListNode node = pListNode.next;
pListNode.value = node.value;
pListNode.next = node.next;
} else if (headListNode == pListNode) { // 只有一个头节点
pListNode = null;
headListNode = null;
} else { // 链表中有多个节点,删除的是尾节点
// 只能遍历了
ListNode node = headListNode;
while (node.next != pListNode) {
node = node.next;
}
node.next = null;
pListNode = null;
}
}
private static void deleteDuplication(ListNode headListNode) {
if (headListNode == null) return;
ListNode preNode = null;
ListNode pNode = headListNode;
while (pNode != null) {
ListNode pNext = pNode.next;
boolean needDelete = false;
if (pNext != null && pNext.value == pNode.value) needDelete = true;
if (!needDelete) {
preNode = pNode;
pNode = pNext;
} else {
int value = pNode.value;
ListNode pDelNode = pNode;
while (pDelNode != null && pDelNode.value == value) {
pNext = pDelNode.next;
pDelNode = pNext;
}
if (preNode == null) headListNode = pNext;
else pNode.next = pNext;
// else preNode.next = pNext;
// pNode = pNext;
}
}
}
public static void printListNode(ListNode headListnode) {
while (headListnode != null) {
System.out.println(headListnode.value);
headListnode = headListnode.next;
}
}
}