SeaSide-秦时明月

自我勉励 & 共同成长

Swap Nodes in Pairs

 

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

递归again???!!!

public ListNode swapPairs(ListNode head) {
    if ((head == null)||(head.next == null))
        return head;
    ListNode n = head.next;
    head.next = swapPairs(head.next.next);
    n.next = head;
    return n;
}



阅读更多
文章标签: java算法
个人分类: leetcode
上一篇Merge Two Sorted Lists
下一篇杨辉三角
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭