Skip to content

Commit

Permalink
024-swap-nodes-in-pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 11, 2018
1 parent c8a1c96 commit 071b8b5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 024-swap-nodes-in-pairs.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -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..<array.count {
let new = ListNode(array[i])
previous.next = new
previous = new
}
}

func toArray() -> [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()
4 changes: 4 additions & 0 deletions 024-swap-nodes-in-pairs.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 071b8b5

Please sign in to comment.