1.添加虚拟节点
package day03;
public class 移除链表元素1添加虚拟节点 {
public static ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// 因为删除可能涉及到头节点,所以设置dummy节点,统一操作
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;//删除节点current操作
cur = cur.next;
} else {
pre = cur;
cur = cur.next;
}
}
return dummy.next;//注意返回的头节点是虚拟节点的后一个节点
}
public static void main(String[] args) {
// 创建节点
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
// 构建链表
listNode1.next = listNode2;
listNode2.next = listNode3;
// 遍历链表
ListNode current = listNode1;
while (current != null) {
System.out.print(current.val);
current = current.next;
}
System.out.println();
ListNode newHead = removeElements(listNode1, 2);
ListNode current2 = newHead;
while (current2 != null) {
System.out.print(current2.val);
current2 = current2.next;
}
}
}
2.不添加虚拟节点
package day03;
public class 移除链表元素2不添加虚拟节点 {
public static ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
//先要判断头节点是否为value
while (head.val == val) {
if (head.next == null) {
return null;
}
head = head.next;
}
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
cur = cur.next;
} else {
pre = cur;
cur = cur.next;
}
}
return head;
}
public static void main(String[] args) {
// 创建节点
ListNode listNode1 = new ListNode(7);
ListNode listNode2 = new ListNode(7);
ListNode listNode3 = new ListNode(7);
// 构建链表
listNode1.next = listNode2;
listNode2.next = listNode3;
// 遍历链表
ListNode current = listNode1;
while (current != null) {
System.out.print(current.val);
current = current.next;
}
System.out.println();
ListNode newHead = removeElements(listNode1, 7);
ListNode current2 = newHead;
while (current2 != null) {
System.out.print(current2.val);
current2 = current2.next;
}
}
}
二.707.设计链表
package day03;
import com.sun.source.tree.InstanceOfTree;
import org.w3c.dom.Node;
public class SinglyLinkedList {//链表
private Node head;
//内部类:节点
private static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
//向链表头部添加节点
public void addFirst(int value) {
//1.链表为空
// head = new Node(value,null);
//2.链表非空
head = new Node(value, head);
}
//遍历链表
public void loop() {
Node p = head;
while (p != null) {
System.out.println(p.value);
p = p.next;
}
}
//找到末尾节点
private Node findLast() {
if (head == null) {
return null;
}
Node p;
for (p = head; p.next != null; p = p.next) {
}
return p;
}
//向链表末尾添加节点
public void addLast(int value) {
Node last = findLast();
if (last == null) {
addFirst(value);
return;
}
last.next = new Node(value, null);
}
//根据索引找到节点
private Node findNode(int index) {
int i = 0;
for (Node p = head; p != null; p = p.next, i++) {
if (i == index) {
return p;
}
}
return null;
}
//根据索引得到节点的值
public int get(int index) {
Node node = findNode(index);
if (node == null) {
throw illegalIndex(index);
}
return node.value;
}
private static IllegalArgumentException illegalIndex(int index) {
return new IllegalArgumentException(
String.format("index [%d] 不合法%n", index));
}
public void insert(int index, int value) {
if (index == 0) {
addFirst(value);
return;
}
Node prev = findNode(index - 1);
if (prev == null) {
throw illegalIndex(value);
}
prev.next = new Node(value, prev.next);
}
//删除头节点
public void removeFirst() {
if (head == null) {
throw illegalIndex(0);
}
head = head.next;
}
//根据索引删除节点
public void remove(int index) {
if (index == 0) { //index-1不管头节点,要单独处理
removeFirst();
return;
}//细节:对节点都进行非空判断
Node prev = findNode(index - 1);
if (prev == null) { //1
throw illegalIndex(index);
}
Node removed = prev.next;
if (removed == null) { //2
throw illegalIndex(index);
}
prev.next = removed.next;
}
}