题目描述:
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
思路:有三种解题方式,第一种是带虚拟头节点的单链表,第二种是不带虚拟节点的单链表,第三种是利用递归的方式
public class Code_008_RemoveElementsInLinkedList {
public class Node {
public int val;
public Node next;
public Node(int e) {
val = e;
}
/*
测试使用
public Node(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("arr can not be empty");
}
this.val = arr[0];
Node cur = this;
for(int i = 1; i < arr.length; i++) {
cur.next = new Node(arr[i]);
cur = cur.next;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
Node cur = this;
while(cur != null) {
res.