[LintCode] Delete Node in the Middle of Singly Linked List

397 查看

Problem

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

Note

就是把node.next.val赋给node,然后删掉node.next,用node直接连接node.next.next

Solution

public class Solution {
    public void deleteNode(ListNode node) {
        if (node == null) return;
        if (node.next != null) {
            node.val = node.next.val;
            node.next = node.next.next;
        }
        return;
    }
}