Created
January 19, 2020 12:40
-
-
Save spurscho/c5318d5984cf666bd216aec583e45115 to your computer and use it in GitHub Desktop.
24. Swap Nodes in Pairs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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