From 071b8b5fd0ce8a33f7299ca5a48d59827a352499 Mon Sep 17 00:00:00 2001 From: Bofei Zhu Date: Sat, 11 Aug 2018 19:58:52 -0400 Subject: [PATCH] 024-swap-nodes-in-pairs --- .../Contents.swift | 62 +++++++++++++++++++ .../contents.xcplayground | 4 ++ README.md | 1 + 3 files changed, 67 insertions(+) create mode 100644 024-swap-nodes-in-pairs.playground/Contents.swift create mode 100644 024-swap-nodes-in-pairs.playground/contents.xcplayground diff --git a/024-swap-nodes-in-pairs.playground/Contents.swift b/024-swap-nodes-in-pairs.playground/Contents.swift new file mode 100644 index 0000000..73225b1 --- /dev/null +++ b/024-swap-nodes-in-pairs.playground/Contents.swift @@ -0,0 +1,62 @@ +/// 24. Swap Nodes in Pairs +/// Given a linked list, swap every two adjacent nodes and return its head. +/// +/// Example: +/// Given 1->2->3->4, you should return the list as 2->1->4->3. +/// - Note: +/// Your algorithm should use only constant extra space. +/// You may not modify the values in the list's nodes, only nodes itself may be changed. + +import XCTest + +func swapPairs(_ head: ListNode?) -> ListNode? { + let dummyHead = ListNode(0) + dummyHead.next = head + var current: ListNode? = dummyHead + while let left = current?.next, + let right = left.next { + left.next = right.next + right.next = left + current?.next = right + current = left + } + return dummyHead.next +} + +final class ListNode { + var val: Int + var next: ListNode? + init(_ val: Int) { + self.val = val + } + + init(array: [Int]) { + guard !array.isEmpty else { fatalError() } + val = array[0] + var previous = self + for i in 1.. [Int] { + var current: ListNode? = self + var array: [Int] = [] + while current != nil { + array.append(current!.val) + current = current?.next + } + return array + } +} + +class Tests: XCTestCase { + func testListNodeInitCorretly() { + let head = ListNode(array: [1, 2, 3, 4]) + XCTAssertEqual(swapPairs(head)?.toArray(), [2, 1, 4, 3]) + } +} + +Tests.defaultTestSuite.run() diff --git a/024-swap-nodes-in-pairs.playground/contents.xcplayground b/024-swap-nodes-in-pairs.playground/contents.xcplayground new file mode 100644 index 0000000..a93d484 --- /dev/null +++ b/024-swap-nodes-in-pairs.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/README.md b/README.md index e154e05..ffb2337 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,4 @@ 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/021-merge-two-sorted-lists.playground/Contents.swift) 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/022-generate-parentheses.playground/Contents.swift) 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/023-merge-k-sorted-lists.playground/Contents.swift) +24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/023-swap-nodes-in-pairs.playground/Contents.swift)