leetcode-面试题18-删除链表的节点
javaO(N)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 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) { ListNode firstNode = new ListNode(-1); firstNode.next = head; ListNode curr = firstNode; while(curr!=null && curr.next != null){ if(curr.next.val == val){ curr.next = curr.next.next; } curr = curr.next; } return firstNode.next; } }