Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. /*
  2. Reverse a linked list and return pointer to the head
  3. The input list will have at least one element
  4. Node is defined as
  5. class Node {
  6. int data;
  7. Node next;
  8. }
  9. */
  10.  
  11. Node Reverse(Node head) {
  12. if(head == null || head.next == null)
  13. return head;
  14.  
  15. Node p = null;
  16. Node tail= null;
  17.  
  18. while(head != null){
  19. p = head.next;
  20. head.next = tail;
  21. tail = head;
  22. head = p;
  23. }
  24.  
  25. return tail;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement