Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
hard

Recursive Reversal of a Doubly Linked List

Mark
< Prev
Next >

What is printed when the following code is executed?

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

3 2 1

B

3 1 2

C

Compilation Error

D

1 2 3

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.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|