Created
June 27, 2014 17:40
-
-
Save yokiy/b5bbba6b2bdb36327a29 to your computer and use it in GitHub Desktop.
cc 2.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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