| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
hard Solved by 1 students
Recursive Reversal of a Doubly Linked List
< Prev
Next >

After executing the reverse method on the doubly linked list, what is the printed output?

class DNode {
    int data;
    DNode prev, next;
    DNode(int d) { data = d; }
}

public class RecursiveReverse {
    public static DNode reverse(DNode node) {
        if(node == null) return null;
        if(node.next == null) {
            node.prev = null;
            return node;
        }
        DNode newHead = reverse(node.next);
        node.next.next = node;
        node.prev = node.next;
        node.next = null;
        return newHead;
    }
    
    public static void main(String[] args) {
        DNode n1 = new DNode(1);
        DNode n2 = new DNode(2);
        DNode n3 = new DNode(3);
        n1.next = n2; n2.prev = n1;
        n2.next = n3; n3.prev = n2;
        DNode head = reverse(n1);
        System.out.println(head.data + " " + head.next.data + " " + head.next.next.data);
    }
}
A

1 2 3

B

3 1 2

C

3 2 1

D

Compilation Error

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy