Linked List Node Swapping
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
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE