Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. public void reverse()
  2. {
  3. Node newHead = null, temp = null;
  4.  
  5. //while the head is not null
  6. while(head!=null){
  7.  
  8. //holding the reference of the newHead
  9. //in a temporary object
  10. temp = newHead;
  11. //i then make the newHead to be the original head
  12. //i do this because then I will make the head be the
  13. //the next one on the list
  14. newHead = head;
  15. head = head.getNext();
  16. //now im setting the next one in the newHead to be the temp
  17. //which holds the reference of newHead before it was updated to head
  18. newHead.setNext(temp);
  19. }
  20.  
  21. //when head equals null I give it the reference to newHead
  22. head = newHead;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement