leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Recursive solution for 2.2.8 Swap Nodes in Pairs

Open Yobretaw opened this issue 10 years ago • 0 comments


ListNode* swapPairs(ListNode *head) {
  if(head == NULL || head->next == NULL)
    return head;

  ListNode *next = head->next;
  ListNode *temp = next->next;
  next->next = head;
  head->next = swapPairs(temp);

  return next;
}

Yobretaw avatar Dec 25 '14 04:12 Yobretaw