力扣:剑指 Offer 18. 删除链表的节点

This commit is contained in:
huangge1199 2021-02-07 14:30:02 +08:00
parent 56c6c43645
commit 6db8c8dfe0

View File

@ -10,4 +10,28 @@ public class DeleteNode {
node.val = node.next.val;
node.next = node.next.next;
}
/**
* 剑指 Offer 18. 删除链表的节点
* @param head
* @param val
* @return
*/
public ListNode deleteNode(ListNode head, int val) {
ListNode temp = head;
ListNode pro = head;
while (temp.val == val) {
head = pro = temp = head.next;
}
temp = temp.next;
while (temp != null) {
if (temp.val == val) {
pro.next = temp.next;
}else{
pro = temp;
}
temp = temp.next;
}
return head;
}
}