Skip to content

Commit be11f6b

Browse files
committed
Remove loop-detection logic and tests.
1 parent e0b58f6 commit be11f6b

File tree

2 files changed

+4
-83
lines changed

2 files changed

+4
-83
lines changed

Singly Linked List/SinglyLinkedList.swift

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,6 @@ public struct SinglyLinkedList<T>
198198
return self.find(kthToLast: kthToLast, startingAt: self.storage.head, count: UInt(self.count))
199199
}
200200

201-
202-
203-
// MARK: LOOP DETECTION
204-
205-
/// A singly linked list contains a loop if one node references back to a previous node.
206-
///
207-
/// - Returns: Whether the linked list contains a loop
208-
public func containsLoop() -> Bool
209-
{
210-
/// Advances a node at a time
211-
var current = self.storage.head
212-
213-
/// Advances twice as fast
214-
var runner = self.storage.head
215-
216-
while (runner != nil) && (runner?.next != nil) {
217-
218-
current = current?.next
219-
runner = runner?.next?.next
220-
221-
if runner === current {
222-
return true
223-
}
224-
}
225-
226-
return false
227-
}
228-
229201
}
230202

231203

@@ -261,25 +233,17 @@ private extension SinglyLinkedList {
261233
// Copy on write: we are about to modify next a property in
262234
// a potentially shared node. Make sure it's new if necessary.
263235
self.storageForWritting.tail?.next = node
264-
if !self.containsLoop() {
265-
let (tail, _) = findTail(in: node)
266-
self.storageForWritting.tail = tail // There
267-
} else {
268-
self.storageForWritting.tail = nil
269-
}
236+
let (tail, _) = findTail(in: node)
237+
self.storageForWritting.tail = tail // There
270238
}
271239
else
272240
{
273241
// This also means that there's no head.
274242
// Otherwise the state would be inconsistent.
275243
// This will be checked when adding and deleting nodes.
276244
self.storageForWritting.head = node
277-
if !self.containsLoop() {
278-
let (tail, _) = findTail(in: node)
279-
self.storageForWritting.tail = tail // There
280-
} else {
281-
self.storageForWritting.tail = nil
282-
}
245+
let (tail, _) = findTail(in: node)
246+
self.storageForWritting.tail = tail // There
283247
}
284248
}
285249

Singly Linked List/Tests/Tests/SinglyLinkedListTests.swift

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,48 +120,6 @@ class SinglyLinkedListTests: XCTestCase {
120120
XCTAssertTrue(list.find(kthToLast: 7)?.value == 2)
121121
XCTAssertTrue(list.find(kthToLast: 8)?.value == 2)
122122
XCTAssertTrue(list.find(kthToLast: 9)?.value == nil)
123-
XCTAssertFalse(list.containsLoop())
124-
}
125-
126-
func testContainsLoop() {
127-
let n1 = SinglyLinkedListNode<Int>(value: 1)
128-
let n2 = SinglyLinkedListNode<Int>(value: 2)
129-
let n3 = SinglyLinkedListNode<Int>(value: 3)
130-
let n4 = SinglyLinkedListNode<Int>(value: 4)
131-
let n5 = SinglyLinkedListNode<Int>(value: 5)
132-
let n6 = SinglyLinkedListNode<Int>(value: 6)
133-
let n7 = SinglyLinkedListNode<Int>(value: 7)
134-
let n8 = SinglyLinkedListNode<Int>(value: 8)
135-
n1.next = n2
136-
n2.next = n3
137-
n3.next = n4
138-
n4.next = n5
139-
n5.next = n6
140-
n6.next = n7
141-
n7.next = n8
142-
n8.next = n2
143-
144-
let list = SinglyLinkedList<Int>(head: n1)
145-
XCTAssertTrue(list.containsLoop())
146-
XCTAssertTrue(list.last == nil)
147-
}
148-
149-
func testContainsLoopFalseWithLiterals() {
150-
let list: SinglyLinkedList<Int> = [1,2,3,4,5,6,7,8]
151-
XCTAssertTrue(list.last == 8, "Found \(String(describing: list.last))")
152-
XCTAssertFalse(list.containsLoop())
153-
}
154-
155-
func testContainsLoopFalseWithNodes() {
156-
let n1 = SinglyLinkedListNode<Int>(value: 1)
157-
let n2 = SinglyLinkedListNode<Int>(value: 2)
158-
let n3 = SinglyLinkedListNode<Int>(value: 3)
159-
n1.next = n2
160-
n2.next = n3
161-
162-
let list = SinglyLinkedList<Int>(head: n1)
163-
XCTAssertFalse(list.containsLoop())
164-
165123
}
166124

167125
func testConstructorFromArrayLiteralWhenEmpty() {
@@ -178,7 +136,6 @@ class SinglyLinkedListTests: XCTestCase {
178136
XCTAssertTrue(list.count == 1, "Found \(list.count)")
179137
}
180138

181-
182139
func testAppendValue() {
183140
var list = SinglyLinkedList<Int>()
184141
list.append(value: 1)

0 commit comments

Comments
 (0)