reversing a linked list in python code example
Example 1: python reverse linked list
def reverseLinkedList(root):
cur = root
prev = None
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
return prev
Example 2: Revese a Linked List
LinkedList ll = new LinkedList<>();
ll.add(1);
ll.add(2);
ll.add(3);
System.out.println(ll);
LinkedList ll1 = new LinkedList<>();
ll.descendingIterator().forEachRemaining(ll1::add);
System.out.println(ll1);