// returns head of new list
public Node reverseInPairs(Node head) {
Node first = head;
Node second = first.next;
if (first == null || second == null) {
return head;
// can't reverse a singleton/empty list. head remains the same
}
while (first != null && second != null) {
temp = first.next.next;
second.next = first;
first.next = temp;
first = temp;
second = first.next;
}
return head.next;
}
What is first and second??
ReplyDelete