Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 845 Bytes

24-Swap-Nodes-in-Pairs.md

File metadata and controls

39 lines (30 loc) · 845 Bytes

24. Swap Nodes in Pairs

2017-03-04

Given a linked list, swap every two adjacent nodes and return its head.

For example, Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Solution

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */
class Solution {
    func swapPairs(_ head: ListNode?) -> ListNode? {
        guard let h = head, let n = head?.next else { return head }
        let v = h.val
        h.val = n.val
        n.val = v
        n.next = swapPairs(n.next)
        return h
    }
}