forked from PratikshaChuryaA/Hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pairwise swap elements of a linked list.cpp
93 lines (73 loc) · 2.03 KB
/
Pairwise swap elements of a linked list.cpp
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
#include <iostream>
// Define a singly linked list node structure
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
// Function to swap nodes pairwise in the linked list
Node* pairwiseSwap(Node* head) {
// If the list is empty or has only one node, no need to swap
if (!head || !head->next) {
return head;
}
// Initialize pointers
Node* newHead = head->next; // New head will be the second node
Node* prev = nullptr;
Node* current = head;
while (current && current->next) {
Node* firstNode = current;
Node* secondNode = current->next;
// Swap nodes
firstNode->next = secondNode->next;
secondNode->next = firstNode;
// Link the previous pair to the current pair
if (prev) {
prev->next = secondNode;
}
// Update prev to the current node (firstNode after swap)
prev = firstNode;
// Move to the next pair
current = firstNode->next;
}
return newHead;
}
// Function to print the linked list
void printList(Node* head) {
while (head) {
std::cout << head->data << " ";
head = head->next;
}
std::cout << std::endl;
}
// Helper function to create a new linked list from an array
Node* createList(const int* arr, int size) {
if (size == 0) return nullptr;
Node* head = new Node(arr[0]);
Node* current = head;
for (int i = 1; i < size; ++i) {
current->next = new Node(arr[i]);
current = current->next;
}
return head;
}
// Cleanup function to free allocated memory
void deleteList(Node* head) {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4};
Node* head = createList(arr, 4);
std::cout << "Original List: ";
printList(head);
head = pairwiseSwap(head);
std::cout << "List after Pairwise Swapping: ";
printList(head);
// Cleanup
deleteList(head);
return 0;
}