-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathContents.swift
67 lines (54 loc) · 1.55 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//: Playground - noun: a place where people can play
import UIKit
precedencegroup LongArrowPrecedence {
associativity: left
higherThan: AssignmentPrecedence
}
infix operator ~>: LongArrowPrecedence
class Node: CustomStringConvertible{
var description: String {
if next == nil { return "\(value)" }
return "\(value) ~> \(next!.description)"
}
var next: Node?
var value: Int
init(_ value: Int) {
self.value = value
}
static func ~>(lhs: Node, rhs: Node) -> Node {
lhs.next = rhs
return rhs
}
}
//Reverse a Linked List in groups of given size | Set 1
//Given a linked list, write a function to reverse every k nodes (where k is an input to the function).
//
//Example:
//Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
//Output: 3->2->1->6->5->4->8->7->NULL.
//
//Inputs: 1->2->3->4->5->6->7->8->NULL and k = 5
//Output: 5->4->3->2->1->8->7->6->NULL.
func reverse(node: Node?, k: Int) -> Node? {
guard let node = node else { return nil }
var boundK = 1
var next = node.next
var previous = node
node.next = nil
for _ in 1 ..< k {
if let nextUnwraped = next {
boundK += 1
next = nextUnwraped.next
nextUnwraped.next = previous
previous = nextUnwraped
}
else {
return reverse(node: current, k: boundK)
}
}
node.next = reverse(node: next, k: k)
return previous
}
let start = Node(0)
start ~> Node(1) ~> Node(2) ~> Node(3) ~> Node(4) ~> Node(5)
reverse(node: start, k: 5)