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.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'swap_nodes_pairs.cpp')
-rw-r--r-- | swap_nodes_pairs.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/swap_nodes_pairs.cpp b/swap_nodes_pairs.cpp new file mode 100644 index 0000000..139c336 --- /dev/null +++ b/swap_nodes_pairs.cpp @@ -0,0 +1,21 @@ +// 24. Swap Nodes in Pairs +#include "leetcode.h" + +/* + * 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) + */ + +class Solution { +public: + ListNode *swapPairs(ListNode *head) { + if (!head || !head->next) + return head; + ListNode *tmp; + tmp = head->next; + head->next = swapPairs(head->next->next); + tmp->next = head; + return tmp; + } +}; |