循环控制-链表删除结点
0.目录
1.删除结点的思路
2.Java代码实现
1.删除结点的思路
链表删除结点的意思就是给定一个值,把链表中与给定值相等的结点全部删除:
例如1 → 2 → 3 → 2 → 5 → Null
,删除给定值2,变为:1 → 3 → 5 → Null
删除的思路:
用一个previous指针指向3,如果3的下一个元素是2,就让3指向5(相当于跳过了2),而previous依旧指向3。
PS:本篇结点和创建链表的实现同前文 递归控制-创建链表
2.Java代码实现
2.1 链表删除结点的实现
不考虑头结点时的实现:
public Node deleteIfEquals(Node head, int value) {
Node prev = head;
// Loop invariant: list nodes from head up to prev has been processed.
// (Nodes with values equal to value are deleted.)
while (prev.getNext() != null) {
if (prev.getNext().getValue() == value) {
// delete it
prev.setNext(prev.getNext().getNext());
} else {
prev = prev.getNext();
}
}
return head;
}
2.2 头结点怎么办
头结点没有previous怎么办?
- 特殊处理
- 增加虚拟头结点
此处采用特殊处理头结点(在Node prev = head;
前面加上以下代码):
if (head == null) {
return null;
}
if (head.getValue() == value) {
head = head.getNext();
}
但是程序依然有问题,比如说2 → 2 → 3 → 2 → 5 → Null
,这样开头有两个2,执行以上程序后只会删除第一个2。
优化一下:
if (head == null) {
return null;
}
while (head.getValue() == value) {
head = head.getNext();
}
会发现程序依然有问题,比如说2 → 2 → 2 → 2 → 2 → Null
,这样全是2,执行以上程序后head就是null了。这样在while循环中程序会执行null.getValue()
,自然会报错。
再优化一下:
if (head == null) {
return null;
}
while (head != null && head.getValue() == value) {
head = head.getNext();
}
运行程序会看到依然有问题,比如说2 → 2 → 2 → 2 → 2 → Null
,这样全是2,执行以上程序后head就是null了。这样在接下来的程序中会执行null.getNext()
,自然会报错。
再优化一下:
while (head != null && head.getValue() == value) {
head = head.getNext();
}
if (head == null) {
return null;
}
最终的删除节点实现:
public Node deleteIfEquals(Node head, int value) {
while (head != null && head.getValue() == value) {
head = head.getNext();
}
if (head == null) {
return null;
}
Node prev = head;
// Loop invariant: list nodes from head up to prev has been processed.
// (Nodes with values equal to value are deleted.)
while (prev.getNext() != null) {
if (prev.getNext().getValue() == value) {
// delete it
prev.setNext(prev.getNext().getNext());
} else {
prev = prev.getNext();
}
}
return head;
}
2.3 测试用例
测试程序是否正确运行:
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
LinkedListDeletor deletor = new LinkedListDeletor();
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(1, 2, 3, 2, 5)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 2, 3, 2, 2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 2, 2, 2, 2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(1)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(new ArrayList<Integer>()), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 3, 4, 5, 2)), 2));
}
运行结果为
main所在java文件全部代码:
import interview.common.Node;
import interview.recursion.LinkedListCreator;
import java.util.ArrayList;
import java.util.Arrays;
public class LinkedListDeletor {
public Node deleteIfEquals(Node head, int value) {
while (head != null && head.getValue() == value) {
head = head.getNext();
}
if (head == null) {
return null;
}
Node prev = head;
// Loop invariant: list nodes from head up to prev has been processed.
// (Nodes with values equal to value are deleted.)
while (prev.getNext() != null) {
if (prev.getNext().getValue() == value) {
// delete it
prev.setNext(prev.getNext().getNext());
} else {
prev = prev.getNext();
}
}
return head;
}
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
LinkedListDeletor deletor = new LinkedListDeletor();
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(1, 2, 3, 2, 5)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 2, 3, 2, 2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 2, 2, 2, 2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(1)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2)), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(new ArrayList<Integer>()), 2));
Node.printLinkedList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(2, 3, 4, 5, 2)), 2));
}
}