-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap-nodes.in-pairs.js
113 lines (81 loc) · 2.43 KB
/
swap-nodes.in-pairs.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
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.)
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
*/
var swapPairs = function(head) {
if(!head) return head;
if(!head.next) return head;
let p1 = head;
let p2;
let returnNode = new ListNode(-1);
let dummyNode = returnNode;
while(p1) {
p2 = p1.next;
if(p1.next) {
p1.next = p2.next;
p2.next = p1;
dummyNode.next = new ListNode(p2.val);
dummyNode.next.next = new ListNode(p2.next.val);
dummyNode = dummyNode.next.next;
p1 = p1.next;
} else {
dummyNode.next = new ListNode(p1.val);
return returnNode.next;
}
}
return returnNode.next;
};
/*
While swapping the nodes I just save the save the value of swapped nodes at that loop
because when the loop goes ahead the pointer also goes ahead so the value returnes is altered so just save values and attach them
to dummy node
*/
/* Other's Solution with recursion */
var swapPairs = function(head) {
if(!head || !head.next) return head;
var v1 = head, v2 = head.next, v3 = v2.next;
v2.next = v1;
v1.next = swapPairs(v3);
return v2;
};
/*
In the given code, the operation v2.next = v1 is part of swapping nodes in a linked list. The purpose of this operation is to reverse the direction of the next pointers of nodes v1 and v2. So, let's see the effect of this operation step-by-step.
Consider the initial state:
rust
1 -> 2 -> 3 -> 4
We have two variables, v1 pointing to node 1 and v2 pointing to node 2.
v2.next = v1: This means we are making v2 point to v1. After this step:
rust
v1
↓
1 <- 2 3 -> 4
^ ^
v1 v2
As you can see, v2 now points to v1, and we have 2 -> 1.
*/
/* Yet another solution */
var swapPairs = function(head) {
if(head == null || head.next == null) {
return head;
}
let ans = new ListNode(0);
ans.next = head;
let curr = ans;
while(curr.next != null && curr.next.next != null) {
let t1 = curr.next;
let t2 = curr.next.next;
curr.next = t2;
t1.next = t2.next;
t2.next = t1;
curr = curr.next.next;
}
return ans.next;
};