Skip to content

Commit dc810d2

Browse files
committed
Modified last and count method after review.
1 parent ec6d34d commit dc810d2

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public final class LinkedList<T> {
4242

4343
/// Computed property to iterate through the linked list and return the last node in the list (if any)
4444
public var last: Node? {
45-
guard let head = head else {
45+
guard var node = head else {
4646
return nil
4747
}
48-
var node = head
48+
4949
while let next = node.next {
5050
node = next
5151
}
@@ -54,10 +54,10 @@ public final class LinkedList<T> {
5454

5555
/// Computed property to iterate through the linked list and return the total number of nodes
5656
public var count: Int {
57-
guard let head = head else {
57+
guard var node = head else {
5858
return 0
5959
}
60-
var node = head
60+
6161
var count = 1
6262
while let next = node.next {
6363
node = next

Linked List/LinkedList.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ public final class LinkedList<T> {
2525
}
2626

2727
public var last: Node? {
28-
if var node = head {
29-
while let next = node.next {
30-
node = next
31-
}
32-
return node
33-
} else {
28+
guard var node = head else {
3429
return nil
3530
}
31+
32+
while let next = node.next {
33+
node = next
34+
}
35+
return node
3636
}
3737

3838
public var count: Int {
39-
guard let head = head else {
39+
guard var node = head else {
4040
return 0
4141
}
42-
var node = head
42+
4343
var count = 1
4444
while let next = node.next {
4545
node = next

0 commit comments

Comments
 (0)