| preferred AP College board partner for AP classes
hard Solved by 2 students
Linked List Node Swapping
< Prev
Next >

What does the program print?

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

3 4

C

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

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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