Created
December 25, 2018 18:41
-
-
Save Ibrahimhass/19a7c9bc0b5d40af37f5d0dea3d0e370 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Get Node at Index | |
extension LinkedList { | |
func getNode(atPosition position: Int) -> Node? { | |
guard position > 0 else { | |
return nil | |
} | |
var counter = 1 | |
var current = head | |
while current != nil && counter <= position { | |
if counter == position { | |
return current } | |
current = current?.next | |
counter += 1 | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment