1. 程式人生 > >leetcode:(24)Swap Nodes In Pairs(java)

leetcode:(24)Swap Nodes In Pairs(java)

package LeetCode_LinkedList;

/**
 * 題目:
 *      Given a linked list, swap every two adjacent nodes and return its head.
 *      Example:
 *          Given 1->2->3->4, you should return the list as 2->1->4->3.
 */
public class SwapNodesInPairs_24_1017 {
    public ListNode SwapNodesInPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode next = head.next;
        head.next = SwapNodesInPairs(head.next.next);
        next.next = head;

        return next;
    }
}