题目描述
代码
class Solution {
public ListNode deleteNode(ListNode head, int val) {
//先判断头结点是否为需删除的点
if(head.val == val){
return head.next;
}
ListNode pre = head;
ListNode cur = head.next;
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
return head;
}else{
cur = cur.next;
pre = pre.next;
}
}
return head;
}
}