Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void reverse()
- {
- Node newHead = null, temp = null;
- //while the head is not null
- while(head!=null){
- //holding the reference of the newHead
- //in a temporary object
- temp = newHead;
- //i then make the newHead to be the original head
- //i do this because then I will make the head be the
- //the next one on the list
- newHead = head;
- head = head.getNext();
- //now im setting the next one in the newHead to be the temp
- //which holds the reference of newHead before it was updated to head
- newHead.setNext(temp);
- }
- //when head equals null I give it the reference to newHead
- head = newHead;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement