【done】剑指offer18:删除链表指定节点

力扣,https://leetcode.cn/problems/shan-chu-lian-biao-de-jie-dian-lcof/description/

// 自己写的答案
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        if (head.val == val) {
            return head.next;
        }
        ListNode pre = head, cur = head.next;
        while (cur != null) {
            if (cur.val != val) {
                pre = cur;
                cur = cur.next;
            } else {
                pre.next = cur.next;
                break;
            }
        }

        return head;
    }
}

你可能感兴趣的:(剑指offer题目笔记,链表)