JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 }

 

posted on 2013-11-05 14:56  JasonChang  阅读(160)  评论(0编辑  收藏  举报