-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSwapNodesInPairs.java
81 lines (70 loc) · 2.37 KB
/
SwapNodesInPairs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*https://leetcode.com/problems/swap-nodes-in-pairs/*/
/*https://binarysearch.com/problems/Pairwise-Linked-List-Swap*/
//iterative solution
class Solution {
public ListNode swapPairs(ListNode head) {
//edge case
if (head == null || head.next == null) return head;
ListNode first = head, second = head, newList = head;
ListNode newHead = head.next;
//till we have more elements left
while (newList != null && newList.next != null)
{
//swap first and second pointers
second = first.next;
newList = second.next;
second.next = first;
//make first.next to point the new list
if (newList != null && newList.next != null)
first.next = newList.next;
else if (newList == null)
first.next = null;
else if (newList.next == null)
first.next = newList;
//update for the next iteration
first = newList;
}
return newHead;
}
}
class Solution {
public LLNode solve(LLNode node) {
if (node == null || node.next == null)
return node;
LLNode prev = node, curr = node, newList = node, newHead = node.next;
while (newList != null && newList.next != null)
{
prev = newList;
curr = prev.next;
newList = curr.next;
curr.next = prev;
prev.next = newList == null || newList.next == null ? newList : newList.next;
}
return newHead;
}
}
//recursive solution
class Solution {
public ListNode swapPairs(ListNode head) {
//edge case
if (head == null || head.next == null) return head;
//recursion call
head = swap(head,head.next);
return head;
}
public ListNode swap(ListNode first, ListNode second) {
//for odd number of nodes straight-away return the remaining list
if (second == null)
return first;
ListNode list = null; //stores the already swapped list
//if list is not already exhausted
if (second.next != null)
/*recursion*/
list = swap(second.next,second.next.next);
//swap and attach the already swapped list
second.next = first;
first.next = list;
//return the list
return second;
}
}