diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /swap_nodes_pairs.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'swap_nodes_pairs.c')
-rw-r--r-- | swap_nodes_pairs.c | 21 |
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; +} |