class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self, input=None):
self.first = None
self.last = None
self.index = None
if input is not None:
for p in input:
new = Node(p)
if self.first is None:
self.first = new
else:
self.last.next = new
self.last= new
This code makes a linked list from parameter.
t = LinkedList([1,2,[3,4],5])
will be stored as: 1-2-[3,4]-5 where - sign is data.next reference. I need reverse of this list, my idea was to swap elements [0]-[n],[1]-[n-1]..
def reverse(self):
temp = self.first
self.first = self.last
self.last = temp
temp = self.first.next
It swaps first and last element and makes temp as next node from start, but I don't know how to continue, how to make this code work ?
self.first?Noneto it.xandyin Python without a temp variable using the statementx, y = y, x.