Skip to content

Commit

Permalink
Use ReversedRandomAcces protocol directly instead of casting it int…
Browse files Browse the repository at this point in the history
…o `Array`
  • Loading branch information
farzadshbfn committed Mar 8, 2017
1 parent e734bbc commit f622eff
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class LinkedList<T> {
}

init?(array: [T]) {
let reversed = Array(array.reversed())
let reversed = array.reversed()
guard let first = array.first else {
return nil
}

var tailLinkedList: LinkedList?

for i in 0 ..< reversed.count - 1 {
tailLinkedList = LinkedList(next: tailLinkedList, value: reversed[i])
tailLinkedList = LinkedList(next: tailLinkedList, value: reversed.itemOnStartIndex(advancedBy: i))
}

self.next = tailLinkedList
Expand Down Expand Up @@ -51,15 +51,15 @@ class DoublyLinkedList<T> {
}

init?(array: [T]) {
let reversed = Array(array.reversed())
let reversed = array.reversed()
guard let first = array.first else {
return nil
}

var tailDoublyLinkedList: DoublyLinkedList?

for i in 0 ..< reversed.count - 1 {
let nextTail = DoublyLinkedList(next: tailDoublyLinkedList, value: reversed[i])
let nextTail = DoublyLinkedList(next: tailDoublyLinkedList, value: reversed.itemOnStartIndex(advancedBy: i))
tailDoublyLinkedList?.previous = nextTail
tailDoublyLinkedList = nextTail
}
Expand Down

0 comments on commit f622eff

Please sign in to comment.