lintcode：在O(1)时间复杂度删除链表节点

给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。并在删除该节点后，返回表头。

```        node.val = node.next.val;
node.next = node.next.next;```

Java程序：

```/**
* 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)
return;
node.val = node.next.val;
node.next = node.next.next;

}
}```

View Code

Python程序：

```"""
Definition of ListNode
class ListNode(object):

def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
# @param node: the node in the list should be deleted
# @return: nothing
def deleteNode(self, node):
# write your code here
if node==None:
return
node.val = node.next.val
node.next = node.next.next```

View Code

原文作者：水滴失船
原文地址: https://www.cnblogs.com/theskulls/p/4871969.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。