• + 0 comments

    This is the easiest and most efficient way I've found to reverse a doubly linked list in python 3

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    class DoublyLinkedListNode:
        def __init__(self, node_data):
            self.data = node_data
            self.next = None
            self.prev = None
    
    class DoublyLinkedList:
        def __init__(self):
            self.head = None
            self.tail = None
    
        def insert_node(self, node_data):
            node = DoublyLinkedListNode(node_data)
    
            if not self.head:
                self.head = node
            else:
                self.tail.next = node
                node.prev = self.tail
    
    
            self.tail = node
    
    def print_doubly_linked_list(node, sep, fptr):
        while node:
            fptr.write(str(node.data))
    
            node = node.next
    
            if node:
                fptr.write(sep)
    
    #
    # Complete the 'reverse' function below.
    #
    # The function is expected to return an INTEGER_DOUBLY_LINKED_LIST.
    # The function accepts INTEGER_DOUBLY_LINKED_LIST llist as parameter.
    #
    
    #
    # For your reference:
    #
    # DoublyLinkedListNode:
    #     int data
    #     DoublyLinkedListNode next
    #     DoublyLinkedListNode prev
    #
    #
    
    def reverse(head):
        # Write your code here
        if head is None:
            return None
        curr = head
        while curr:
            curr.prev, curr.next = curr.next, curr.prev
            head = curr
            curr = curr.prev
        return head
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input())
    
        for t_itr in range(t):
            llist_count = int(input())
    
            llist = DoublyLinkedList()
    
            for _ in range(llist_count):
                llist_item = int(input())
                llist.insert_node(llist_item)
    
            llist1 = reverse(llist.head)
    
            print_doubly_linked_list(llist1, ' ', fptr)
            fptr.write('\n')
    
        fptr.close()