# _*_coding:utf-8 _*_
class ListNode:
def __init__(self):
self.val = None
self.next = None
class Solution:
def __init__(self):
return
def list_generate(self, lst):
"""
生成链表
"""
if not lst:
return None
list_node = ListNode()
list_node.val = lst[0]
if len(lst) == 1:
list_node.next = None
else:
list_node.next = self.list_generate(lst[1:])
return list_node
def delete_node(self, head, del_node):
"""
删除指定节点
"""
if not head or not del_node:
return False
if del_node == head:
# 一个节点——删除头节点
head = None
elif del_node.next:
# 多个节点——删除的节点不是尾节点
next_node = del_node.next
del_node.val = next_node.val
del_node.next = next_node.next
next_node.next = None
else:
# 多个节点——删除节点为尾节点
cur = head
while cur.next != del_node:
cur = cur.next
cur.next = None
return head
if __name__ == '__main__':
solution = Solution()
head_node = solution.list_generate(['a','b','c']) # 测试用例
head_node = solution.delete_node(head_node, head_node.next.next)
node = head_node
if node:
while node:
print node.val,
node = node.next
if node:
print '->',
else:
print 'wrong'