Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
hard Solved by 2 students

Linked List Node Swapping

Mark
< Prev
Next >

What is printed when the main method of the following SwapTest class is executed?

public class SwapTest {
    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }
    public static Node swapIfEven(Node head) {
        if(head != null && head.next != null && head.data % 2 == 0) {
            Node temp = head;
            head = head.next;
            temp.next = head.next;
            head.next = temp;
        }
        return head;
    }
    public static void main(String[] args) {
        Node head = new Node(4);
        head.next = new Node(3);
        head = swapIfEven(head);
        System.out.print(head.data + " " + head.next.data);
    }
}
A

4 3

B

4 4

C

3 4

D

3 4 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|