LeetCode - 237. Delete Node in a Linked List

链接

237. Delete Node in a Linked List

题意

删除指定的单向链表结点(除了尾结点)

思路

因为是单向链表,并不知道前序元素。所以不能将前序元素指向node的后继结点,应该转换思路,让node的值改为后继结点 的值,然后指向后继结点的后继结点。

代码

Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

posted @ 2017-05-18 12:43  zyoung  阅读(98)  评论(0编辑  收藏  举报