0
0
DSA Pythonprogramming~20 mins

Reverse a Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reversing a doubly linked list
What is the printed state of the doubly linked list after reversing it using the given code?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

def reverse(head):
    current = head
    temp = None
    while current:
        temp = current.prev
        current.prev = current.next
        current.next = temp
        current = current.prev
    if temp:
        head = temp.prev
    return head

# Create doubly linked list: 1 <-> 2 <-> 3 <-> 4
head = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
head.next = node2
node2.prev = head
node2.next = node3
node3.prev = node2
node3.next = node4
node4.prev = node3

# Reverse the list
head = reverse(head)

# Print list forward
result = []
current = head
while current:
    result.append(current.data)
    current = current.next
print(result)
A[4, 3, 2, 1]
B[1, 2, 3, 4]
C[4, 2, 3, 1]
D[1, 3, 2, 4]
Attempts:
2 left
💡 Hint
Think about how the prev and next pointers are swapped for each node.
🧠 Conceptual
intermediate
1:30remaining
Understanding pointer changes in reversing a doubly linked list
When reversing a doubly linked list, which pointers are swapped for each node?
ASwap the data values of adjacent nodes
BSwap only the prev pointers of each node
CSwap only the next pointers of each node
DSwap the next and prev pointers of each node
Attempts:
2 left
💡 Hint
Think about what makes the list reverse direction.
🔧 Debug
advanced
2:00remaining
Identify the error in this reverse function for doubly linked list
What error will this code produce when reversing a doubly linked list?
DSA Python
def reverse(head):
    current = head
    while current:
        temp = current.next
        current.prev, current.next = current.next, current.prev
        current = temp
    return head
ASyntaxError due to invalid tuple assignment
BThe function returns the original head, not the new head after reversal
CInfinite loop because current never becomes None
DTypeError because prev and next are not swappable
Attempts:
2 left
💡 Hint
Check what happens to the head pointer after swapping pointers.
Predict Output
advanced
1:30remaining
Output after reversing a doubly linked list with one node
What is the output after reversing a doubly linked list with a single node containing data 10?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

def reverse(head):
    current = head
    temp = None
    while current:
        temp = current.prev
        current.prev = current.next
        current.next = temp
        current = current.prev
    if temp:
        head = temp.prev
    return head

head = Node(10)
head = reverse(head)

result = []
current = head
while current:
    result.append(current.data)
    current = current.next
print(result)
A[0]
B[]
C[10]
D[None]
Attempts:
2 left
💡 Hint
Consider how reversing a single node list affects pointers.
🚀 Application
expert
1:00remaining
Number of nodes after reversing a doubly linked list
After reversing a doubly linked list with 7 nodes, how many nodes does the reversed list contain?
A7
B0
C1
D14
Attempts:
1 left
💡 Hint
Reversing changes order but not the number of nodes.