-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-2.java
43 lines (32 loc) · 882 Bytes
/
Problem-2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Pairwise swap elements of a linked list
Problem Link :- https://practice.geeksforgeeks.org/problems/pairwise-swap-elements-of-a-linked-list-by-swapping-data/1
*/
class Solution {
// Function to pairwise swap elements of a linked list.
// It should returns head of the modified list
public Node pairwiseSwap(Node node)
{
// code here
Node current= node;
int k=2;
int count=0;
Node prev=null;
while(current!=null && count<k)
{
Node temp=current;
current=current.next;
temp.next=prev;
prev=temp;
count++;
}
if(current!=null)
{
node.next=pairwiseSwap(current);
}
return prev;
}
}