Skip to content

Instantly share code, notes, and snippets.

@spurscho
Created January 19, 2020 12:40
Show Gist options
  • Save spurscho/c5318d5984cf666bd216aec583e45115 to your computer and use it in GitHub Desktop.
Save spurscho/c5318d5984cf666bd216aec583e45115 to your computer and use it in GitHub Desktop.
24. Swap Nodes in Pairs
class Solution {
func swapPairs(_ head: ListNode?) -> ListNode? {
guard let newHead = head?.next else {
return head
}
// 1 -> 2 -> 3 -> 4
// 2 -> 1 -> 4 -> 3
let tmp = head?.next?.next
head?.next?.next = head
head?.next = swapPairs(tmp)
return newHead
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment