-
Notifications
You must be signed in to change notification settings - Fork 3
/
Reverse a linked list.swift
101 lines (76 loc) · 2.55 KB
/
Reverse a linked list.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Foundation
final class SinglyLinkedListNode {
var data: Int
var next: SinglyLinkedListNode?
public init(nodeData: Int) {
self.data = nodeData
}
}
final class SinglyLinkedList {
var head: SinglyLinkedListNode?
var tail: SinglyLinkedListNode?
public init() {}
public func insertNode(nodeData: Int) {
self.insertNode(node: SinglyLinkedListNode(nodeData: nodeData))
}
private func insertNode(node: SinglyLinkedListNode) {
if let tail = tail {
tail.next = node
} else {
head = node
}
tail = node
}
}
func printSinglyLinkedList(head: SinglyLinkedListNode?, sep: String, fileHandle: FileHandle) {
var node = head
while node != nil {
fileHandle.write(String(node!.data).data(using: .utf8)!)
node = node!.next
if node != nil {
fileHandle.write(sep.data(using: .utf8)!)
}
}
}
/*
* Complete the 'reverse' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/
func reverse(llist: SinglyLinkedListNode?) -> SinglyLinkedListNode? {
var node = llist
var arr: [Int] = []
var nodeArr: [SinglyLinkedListNode] = []
while node != nil {
arr.append(node!.data)
node = node?.next
}
while arr.count > 0 {
let data = arr.popLast()!
let newNode = SinglyLinkedListNode(nodeData: data)
nodeArr.append(newNode)
}
for index in 0..<nodeArr.count - 1 {
nodeArr[index].next = nodeArr[index + 1]
}
return nodeArr.first ?? nil
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let tests = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for testsItr in 1...tests {
guard let llistCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
let llist = SinglyLinkedList()
for _ in 1...llistCount {
guard let llistItem = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
llist.insertNode(nodeData: llistItem)
}
let llist1 = reverse(llist: llist.head!)
printSinglyLinkedList(head: llist1, sep: " ", fileHandle: fileHandle)
fileHandle.write("\n".data(using: .utf8)!)
}