What are the errors is my code?
Output is coming same as the input.
I can’t find any errors.
I was expecting a perfect answer.
I trying to do this question through recursion only.
Code————–
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null || head.next!=null){
return head;
}
ListNode node1=head;
ListNode node2=head.next;
head=recursion(node1,node2);
return head;
}
public ListNode recursion(ListNode node1,ListNode node2){
if(node2==null){
return node2;
}
if(node2.next==null){
node2.next=node1;
node1.next=null;
return node2;
}
ListNode head=recursion(node2.next,node2.next.next);
node2.next=node1;
node1.next=head;
return node2;
}
}
Gaurav Rawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.