Skip to content

Instantly share code, notes, and snippets.

@yokiy
Created June 27, 2014 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokiy/b5bbba6b2bdb36327a29 to your computer and use it in GitHub Desktop.
Save yokiy/b5bbba6b2bdb36327a29 to your computer and use it in GitHub Desktop.
cc 2.3
#2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
from LinkedList import LinkedList, Node
def deleteNode(node):
if node is None or node.next is None:
return False
node.data = node.next.data
node.next = node.next.next
return True
#test
a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
a.next = b
b.next = c
c.next = d
test = LinkedList(a,d)
test.printList()
deleteNode(c)
print 'after delete c,the list is'
test.printList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment