leetcode-面试题18-删除链表的节点

javaO(N)

/**
 * 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;
    }
}
java

猜你喜欢

转载自www.cnblogs.com/oldby/p/12683014.html