Java
Java copied to clipboard
[FEATURE REQUEST] Add Swap Nodes in Pairs Problem in recursion .
What would you like to Propose?
A solution which can be understood by all.
Issue details
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.
class Solution { public ListNode swapPairs(ListNode head) { if(head==null) return head; ListNode temp1=head; ListNode temp2=head; temp1=temp1.next; if(temp2.next!=null) temp2.next=swapPairs(temp2.next.next); if(temp1!=null){ temp1.next=temp2; head=temp1;} return head; } }
Additional Information
No response