Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public Node reverseList(Node head){
  2. // Give resolution here. Please make sure your resolution covers all above-listed test cases.
  3.  
  4. if(head==null) return null; //This is for the case when the list is empty
  5. if(head.next== null)return head; //This is when the list has only one element
  6. else{ //The else block is for when there are two or more nodes
  7. Node newlist=new Node();
  8. newlist.data=head.data;
  9. newlist.next=null;
  10. head=head.next;
  11. while(head!=null){
  12. Node temp=new Node();
  13. temp.data=head.data;
  14. temp.next=newlist; //just reversing the pointers and taking the head to
  15. newlist=temp; //the last element
  16. head=head.next;
  17. }
  18. return newlist;
  19.  
  20. }
  21.  
  22.  
  23.  
  24. // JUnit test cases are not required but you are welcome to provide it to save my time to verify your resolution.
  25. // It is a good habit for the software engineer to prepare JUnit test cases.
  26.  
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement