Проблема в LinkedList не обращая
reversing a linkedlist using 3 pointer? cant understand what is the problem? pls suggest any other iterative solution too.. reversing a linkedlist using 3 pointer? cant understand what is the problem? pls suggest any other iterative solution too.. outcome should be 87654321(reverse of a linkedlist) but currently the output is 1 i think there is problem in reverse function pointers #reversing a linkedlist class Node: def __init__(self,data): self.data=data self.next=None class LinkedList: def __init__(self): self.head=None def push(self,data): new_node=Node(data) new_node.next=self.head self.head=new_node def printlist(self): temp=self.head print() while temp: print(temp.data,end=' ') temp=temp.next def append(self, new_data): new_node = Node(new_data) if self.head is None: self.head = new_node return last = self.head while (last.next): last = last.next last.next = new_node def reverse(self): prev=None current=self.head nnext=current.next while nnext: current.next=prev prev = current current = nnext nnext=nnext.next current.next=prev if __name__=='__main__': llist=LinkedList() llist.append(1) llist.append(2) llist.append(3) llist.append(4) llist.append(5) llist.append(6) llist.append(7) llist.append(8) llist.printlist() llist.reverse() llist.printlist()
Что я уже пробовал:
есть некоторая проблема с указателями моей обратной функции