Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Node reverseList(Node head){
- // Give resolution here. Please make sure your resolution covers all above-listed test cases.
- if(head==null) return null; //This is for the case when the list is empty
- if(head.next== null)return head; //This is when the list has only one element
- else{ //The else block is for when there are two or more nodes
- Node newlist=new Node();
- newlist.data=head.data;
- newlist.next=null;
- head=head.next;
- while(head!=null){
- Node temp=new Node();
- temp.data=head.data;
- temp.next=newlist; //just reversing the pointers and taking the head to
- newlist=temp; //the last element
- head=head.next;
- }
- return newlist;
- }
- // JUnit test cases are not required but you are welcome to provide it to save my time to verify your resolution.
- // It is a good habit for the software engineer to prepare JUnit test cases.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement