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

java 23 2016-02-29 13:03

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;
    }
}
文章评论