Skip to content

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

@Shreya-1305

Description

@Shreya-1305

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions