-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpairwiseSwapNodes.java
61 lines (54 loc) · 1.48 KB
/
pairwiseSwapNodes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Given a LL, swap nodes pairwise
* i.e swap ith node with (i+1)th node
*
* ip: 1 -> 2 -> 3 -> 4 -> 5 -> 6
* op: 2 -> 1 -> 4 -> 3 -> 6 -> 5
*
* ip: 1 -> 2 -> 3 -> 4 -> 5
* op: 2 -> 1 -> 4 -> 3 -> 5
*
* Method 1:
* begin a ptr from first node, this ptr will move two positions ahead
* before moving forward, swap curr's data and curr's next's data
*
* Time: O(n),Space: O(1)
* if data is an object, then swapping can be costly
* disadvantage: we are swapping data
*
*/
public class pairwiseSwapNodes {
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
printList(head);
pairwiseSwap(head);
printList(head);
}
public static void pairwiseSwap(Node head) {
Node curr = head;
while (curr != null && curr.next != null) {
int x = curr.data;
curr.data = curr.next.data;
curr.next.data = x;
curr = curr.next.next;
}
}
public static void printList(Node head) {
Node traverse = head;
while (traverse != null) {
System.out.print(traverse.data + "->");
traverse = traverse.next;
}
System.out.print("null\n");
}
}
/**
* op
* 1->2->3->4->5->6->null
* 2->1->4->3->6->5->null
*/