-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion24.java
More file actions
59 lines (52 loc) · 965 Bytes
/
Copy pathQuestion24.java
File metadata and controls
59 lines (52 loc) · 965 Bytes
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 24
*/
/*
TestCases-
*/
public class Question24
{
public static ListNode swapPairs(ListNode head)
{
if(head==null || head.next==null)
return head;
ListNode p1 = head;
ListNode p2 = head.next;
ListNode temp,temp2=p1,start=p2;
boolean flag = true;
while(p1!=null && p2!=null)
{
if(flag)
{
temp = p2.next;
p2.next = p1;
p1.next = temp;
temp2 = p1;
flag = false;
}
else
{
temp = p2.next;
p2.next = p1;
p1.next = temp;
temp2.next = p2;
temp2 = p1;
}
if(temp==null)
break;
p1 = temp;
p2 = p1.next;
}
return start;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
ListNode head = LinkedList.create(15,"sorted");
LinkedList.display(head);
head = swapPairs(head);
LinkedList.display(head);
}
}