024.Swap Nodes In Pairs

Test cases

1
2
3
4
5
6
[]
[1]
[1,2]
[1,2,3]
[1,2,3,4]
[1,2,3,4,5]

Solution1: accepted

Primitive brute solution. 1ms but is below 99%.

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
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode result = new ListNode(0);
ListNode operation = result;
ListNode even = head.next;
ListNode odd = head;
boolean terminate = false;
while (even != null || odd != null) {
operation.next = new ListNode(even.val);
operation = operation.next;
if (even.next != null && even.next.next != null)
even = even.next.next;
else
terminate = true;
operation.next = new ListNode(odd.val);
operation = operation.next;
if (odd.next != null && odd.next.next != null)
odd = odd.next.next;
else
terminate = true;
if (terminate)
break;
// System.out.println(head);
}
if (odd != null && odd.next == null)
operation.next = new ListNode(odd.val);
return result.next;
}
}

Solution 2: accepted

Bring forward the second number and remove it from the first listnode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode operator = dummy;
while(operator.next != null && operator.next.next != null) {
ListNode odd = operator.next;
ListNode even = operator.next.next;
operator.next = even;
odd.next = even.next; // Remove the used additional even
operator.next.next = odd;
operator = operator.next.next;
}
return dummy.next;
}
}

Solution 3: accepted

1
2
3
4
5
6
7
8
9
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return head;
ListNode nextNode = head.next;
head.next = swapPairs(nextNode.next);
nextNode.next = head;
return nextNode;
}
}