Algorithm Puzzles: Swap Nodes in Pairs
Algorithm Puzzles everyday every week sometimes: Algorithm Puzzles: Swap Nodes in Pairs
Puzzle
Puzzle from leetcode:
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)
Solution
Easy to solve via recursion:
1 | class Solution { |
T.C. should be O(n)
, S.C. should be O(n)
as well