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
| class Solution { ListNode root = null; public ListNode swapPairs(ListNode head) { if(head == null){ return head; }
return dfs(head); }
public ListNode dfs(ListNode root){ if(root == null){ return root; }
ListNode node1 = new ListNode(root.val);
if(root.next == null){ return node1; }
ListNode node2 = new ListNode(root.next.val); node2.next = node1; node1.next = dfs(root.next.next);
return node2; } }
|