题目描述

// 力扣
// 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
// 返回删除后的链表的头节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
题解
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if (head == null)
return null;
if (head.next == null)
return null;
if (head.val == val && head.next != null)
return head.next;
ListNode pre = new ListNode(0);
pre.next = head;
ListNode cur = head;
while (cur != null) {
if (cur.val == val && cur.next != null) {
pre.next = cur.next;
cur = null;
break;
}
if (cur.val == val && cur.next == null) {
pre.next = null;
break;
}
pre = pre.next;
cur = cur.next;
}
return head;
}
}
////////////////////////////// 递归法 //////////////////////////////
class Solution {
// 解题函数,也是递归函数
// 递归函数检查head.next是否为待删除结点
public ListNode deleteNode(ListNode head, int val) {
// 以下三个if语句跟双指针法是一样的,但是意义不一样
// 若head第一个结点就不存在,返回null
if (head == null)
return null;
// 1.当deleteNode还没被递归调用时:
// 此时head还指向头结点,如果head.next为null,说明整个链表就一个结点,删除之后为null
// 2.当deleteNode被递归调用后:
// 由于递归调用的语句是head.next = deleteNode(head.next, val);
// 因此字段head实际表示head.next,那字段head.next实际表示head.next.next,
// 若head.next.next为null,说明head.next是待删除尾结点,直接返回null,
// 这时候head.next会被指向这个返回的null,删除完成
// (注:如果这句if能触发,说明在head.next到达尾结点之前一直都没return过
// 说明尾结点必为待删除结点)
if (head.next == null)
return null;
// 1.当deleteNode还没被递归调用时:
// 如果待删除节点是头结点,直接返回head.next
// 2.当deleteNode被递归调用后:
// head实际表示head.next,所以如果head.next.val == val
// 说明head.next是待删除结点,返回head.next,也就是返回
// head.next.next,这时head.next会指向这个返回的head.next.next
// 原来的head.next从链表中被移除。
if (head.val == val && head.next != null)
return head.next;
// 将head.next指向deleteNode的返回值
// deleteNode将被递归调用,输入为head.next和val
head.next = deleteNode(head.next, val);
// 递归完成,返回删除好的链表头索引head
return head;
}
}