Java icon indicating copy to clipboard operation
Java copied to clipboard

[FEATURE REQUEST] Add Swap Nodes in Pairs Problem in recursion .

Open Shreya-1305 opened this issue 4 months ago • 2 comments

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

Shreya-1305 avatar Oct 07 '24 14:10 Shreya-1305