Offer18.删除链表的节点/Solution.java
/**
 * 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) {
        if (head == null) {
            return null;
        }
        if (head.val == val) {
            return head.next;
        }

        ListNode pre = head;
        ListNode tmp = head.next;

        while (tmp != null) {
            if (tmp.val == val) {
                pre.next = tmp.next;
                return head;
            }
            pre = pre.next;
            tmp = tmp.next;
        }
        return head;
    }
}