Last active
December 10, 2015 11:28
-
-
Save Aaron1011/4427456 to your computer and use it in GitHub Desktop.
Reverse a linked list
This file contains hidden or 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
import copy | |
class Node: | |
def __init__(self, data, next=None): | |
self.data = data | |
self.next = next | |
def __str__(self): | |
return "<Node %s>" % self.data | |
def linkedlreverse(start, end=None, last=None): | |
if start.next == end: | |
start.next = last | |
return start | |
start_copy = copy.copy(start) | |
start.next = last | |
print "Start: ", start, start.next | |
return linkedlreverse(start_copy.next, end, start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment