[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/git-jhy/JavaArithmetic/master/src/LeetCode/LeetCode237.java [Back]  [Original]

package LeetCode;

public class LeetCode237 {

    // 237. Delete Node in a Linked List
    // https://leetcode.com/problems/delete-node-in-a-linked-list/description/
    // : O(1)
    // : O(1)


    // --->
    // 
        // 1. 
        // 2. 
        // 3. 
    public void deleteNode(ListNode node) {

        // : node
        // node.next, null, node
        if (node == null || node.next == null)
            throw new IllegalArgumentException("node should be valid and can not be the tail node.");

        node.val = node.next.val;
        node.next = node.next.next;
    }

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4};

        ListNode head = new ListNode(arr);
        System.out.println(head);

        ListNode node2 = head.findNode(2);
        (new LeetCode237()).deleteNode(node2);
        System.out.println(head);
    }
}

Web Proxy Viewer  |  New URL  |  Original Page