-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path24-swap-nodes-in-pairs.java
More file actions
127 lines (108 loc) · 3.83 KB
/
24-swap-nodes-in-pairs.java
File metadata and controls
127 lines (108 loc) · 3.83 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
// Approach 1: Iterative
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while (prev.next != null && prev.next.next != null) {
ListNode first = prev.next;
ListNode second = prev.next.next;
// Swapping
first.next = second.next;
second.next = first;
prev.next = second;
// Move to the next pair
prev = first;
}
return dummy.next;
}
// Approach 2: Recursive
public ListNode swapPairsRecursive(ListNode head) {
// Base case: no node or only one node, nothing to swap
if (head == null || head.next == null) {
return head;
}
// Nodes to be swapped
ListNode first = head;
ListNode second = head.next;
// Recurse for the rest of the list
first.next = swapPairsRecursive(second.next);
second.next = first;
// New head of the swapped pair is 'second'
return second;
}
// Helper function to print the list
public static void printList(ListNode head) {
ListNode current = head;
System.out.print("[");
while (current != null) {
System.out.print(current.val);
if (current.next != null) {
System.out.print(",");
}
current = current.next;
}
System.out.println("]");
}
// Helper function to create a list from an array
public static ListNode createList(int[] arr) {
if (arr == null || arr.length == 0) {
return null;
}
ListNode head = new ListNode(arr[0]);
ListNode current = head;
for (int i = 1; i < arr.length; i++) {
current.next = new ListNode(arr[i]);
current = current.next;
}
return head;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println("Testing Swap Nodes in Pairs (Iterative):\n");
// Test 1
ListNode head1 = createList(new int[]{1, 2, 3, 4});
System.out.print("Input: [1,2,3,4] -> Output: ");
ListNode result1 = sol.swapPairs(head1);
printList(result1); // Expected: [2,1,4,3]
// Test 2
ListNode head2 = createList(new int[]{});
System.out.print("Input: [] -> Output: ");
ListNode result2 = sol.swapPairs(head2);
printList(result2); // Expected: []
// Test 3
ListNode head3 = createList(new int[]{1});
System.out.print("Input: [1] -> Output: ");
ListNode result3 = sol.swapPairs(head3);
printList(result3); // Expected: [1]
// Test 4
ListNode head4 = createList(new int[]{1, 2, 3});
System.out.print("Input: [1,2,3] -> Output: ");
ListNode result4 = sol.swapPairs(head4);
printList(result4); // Expected: [2,1,3]
System.out.println("\nTesting Swap Nodes in Pairs (Recursive):\n");
// Test 5
ListNode head5 = createList(new int[]{1, 2, 3, 4});
System.out.print("Input: [1,2,3,4] -> Output: ");
ListNode result5 = sol.swapPairsRecursive(head5);
printList(result5); // Expected: [2,1,4,3]
// Test 6
ListNode head6 = createList(new int[]{});
System.out.print("Input: [] -> Output: ");
ListNode result6 = sol.swapPairsRecursive(head6);
printList(result6); // Expected: []
}
}