aboutsummaryrefslogtreecommitdiff
path: root/swap_nodes_pairs.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /swap_nodes_pairs.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'swap_nodes_pairs.c')
-rw-r--r--swap_nodes_pairs.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/swap_nodes_pairs.c b/swap_nodes_pairs.c
new file mode 100644
index 0000000..45c5be5
--- /dev/null
+++ b/swap_nodes_pairs.c
@@ -0,0 +1,21 @@
+// 24. Swap Nodes in Pairs
+
+/*
+ * given a linked list, swap every two adjacent nodes and return
+ * its head. problem must be solved without modifying values
+ * in the list's nodes (ie only nodes themselves may be changed)
+ */
+
+struct ListNode {
+ int val;
+ struct ListNode *next;
+};
+
+struct ListNode *swapPairs(struct ListNode *head) {
+ if (!head || !head->next)
+ return head;
+ struct ListNode *tmp = head->next;
+ head->next = swapPairs(head->next->next);
+ tmp->next = head;
+ return tmp;
+}