The following function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?
Java
class Node {
int value;
Node next;
}
void rearrange(Node list) {
Node p, q;
int temp;
if (list == null || list.next == null) {
return;
}
p = list;
q = list.next;
while (q != null) {
temp = p.value;
p.value = q.value;
q.value = temp;
p = q.next;
q = p != null ? p.next : null;
}
}