剑指offer python版 在O(1)时间删除链表结点

class ListNode(object):
    def __init__(self,x):
        self.val=x
        self.next=None
        
def del_link(head,node):
    if head==node:
        del node
        
    if node.next==None:
        while head:
            if head.next==node:
                head.next=None
                
            head=head.next
            
    else:
        node.val=node.next.val
        n_node=node.next
        node.next=n_node.next
        del n_node
node1 = ListNode(1)
node1.next = ListNode(2)
node1.next.next = ListNode(3)

del_link(node1,ListNode(2))

print(node1.next.val)        
    

 

posted @ 2018-10-25 14:18  findtruth123  阅读(521)  评论(0编辑  收藏  举报