recursion programming
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public ListNode swapPairs(ListNode head) { 14 // IMPORTANT: Please reset any member data you declared, as 15 // the same Solution instance will be reused for each test case. 16 if(head == null) 17 return null; 18 if(head.next == null) 19 return head; 20 ListNode first = head; 21 ListNode second = head.next; 22 ListNode third = second.next; 23 second.next = first; 24 first.next = swapPairs(third); 25 return second; 26 } 27 }