1、前言
题目描述2、思路
声明一个 dummy 节点方便一点
3、代码
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while(head != null && head.val != val){
head = head.next;
pre = pre.next;
}
pre.next = head.next;
head.next = null;
return dummy.next;
}
}
网友评论