Skip to content

Instantly share code, notes, and snippets.

@Ibrahimhass
Created December 25, 2018 18:41
Show Gist options
  • Save Ibrahimhass/19a7c9bc0b5d40af37f5d0dea3d0e370 to your computer and use it in GitHub Desktop.
Save Ibrahimhass/19a7c9bc0b5d40af37f5d0dea3d0e370 to your computer and use it in GitHub Desktop.
//: 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