Code snippets are below: trying to reverse my list of nodes but when I do so, only one node (the first in the linked list) prints. Any idea what I'm doing wrong here? I've written this out on paper and it seems like it should loop through my nodes, adding each one into my new linked list?
# node class
class Node(object):
def __init__(self, value, next=None):
self.value = value
self.next = next
# singly linked list class
class SinglyLinkedList(object):
def __init__(self):
self.head = None
self.tail = None
# I'm trying to do the same thing in my reverseList() method
# as I'm doing in the addFront method
def addFront(self, value):
# create new node
newNode = Node(value)
# set old head to point to new node
if self.head == None:
self.head = newNode
self.tail = newNode
else:
# store old head
last_head = self.head
# set head to new node
self.head = newNode
# point head to old head
self.head.next = last_head
# reverseList() method not working?
# Only giving me first head value? Why?
def reverseList(self):
node = self.head
newList = SinglyLinkedList()
newList.head = None
while node:
if node.next == None:
break
else:
temp = newList.head
newList.head = node
newList.head.next = temp
print newList.head.value
node = node.next