Medium
Subtopics
linked-list·recursion
Companies
adobe·amazon·apple·bloomberg·ebay·facebook·google·lyft·microsoft·oracle·sap·uberLast updated: Aug 2, 2025
public ListNode swapPairs(ListNode head) {
// base case - not enough nodes to swapif (head ==null|| head.next==null) {
return head;
}
ListNode first = head; // first is useless variable as it is same as head, but used for clarity ListNode second = head.next;
ListNode next = second.next;
second.next= first;
first.next= swapPairs(next);
return second;
}