Episode 21 – Linked Lists

The Code Snippets Podcast
The Code Snippets Podcast
Episode 21 - Linked Lists
/

Today I try explain Linked Lists

https://www.youtube.com/watch?v=NhapasNIKuQ

def reverse_list(head)
  current_node = head
  previous_node = nil
    
  until current_node == nil
    # save the value of the next node  
    next_node = current_node.next
      
    # change the pointer of the next node to point back to the node you currently are on 
    current_node.next = previous_node
      
    # update the value of your previous node to be the node you currently are on for the next iteration
    previous_node = current_node
      
    # since we have already update the pointer to the previous node, we need to move onto the next node in our list 
    current_node = next_node  
  end
  
  # once we reach the end of our linked list, we will return the last previous_node which is now the head of our reversed linked list 
  
  previous_node  
    
end