版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
難度:容易
要求:
給定一個單鏈表中的一個等待被刪除的節(jié)點(diǎn)(非表頭或表尾)。請在在O(1)時間復(fù)雜度刪除該鏈表節(jié)點(diǎn)。
樣例
給定 1->2->3->4搞挣,和節(jié)點(diǎn) 3,刪除 3 之后音羞,鏈表應(yīng)該變?yōu)?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) {
// write your code here
if (node == null || node.next == null)
return;
ListNode next = node.next;
node.val = next.val;
node.next = next.next;
return;
}
}