LinkedList---Delete Node in the Middle of Singly Linked List

来源:互联网 发布:知敬畏守底线心得体会 编辑:程序博客网 时间:2024/05/16 07:50

Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.

Have you met this question in a real interview? Yes
Example
Tags
Related Problems
Notes
Given 1->2->3->4, and node 3. return 1->2->4

/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {    /**     * @param node: the node in the list should be deleted     * @return: nothing     */    public void deleteNode(ListNode node) {        node.val = node.next.val;        node.next = node.next.next;    }}
0 0