Swap Nodes in Pairs

转载 2016年06月01日 22:44:12

 

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;
}



举报

相关文章推荐

Swap Nodes in Pairs (LeetCode Algorithm Problems)

这道题应该是一道编程语言题,因为要解决的问题是链表的操作。原题链接Given a linked list, swap every two adjacent nodes and return its h...

LeetCode | Swap Nodes in Pairs(将链表中的元素两两交换)

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, ...

Swap Nodes in Pairs--LeetCode

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->...

LeetCode24. Swap Nodes in Pairs

LeetCode24. Swap Nodes in Pairs

Swap Nodes in Pairs

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next...

Leetcode-Swap Nodes in Pairs

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23793109 时间:2014-4-...

LeetCode - Swap Nodes in Pairs

题注 这道题吧,看起来挺简单的,但是里面涉及到一些细节问题,很容易就错了,在实现之前最好仔细考虑一下各个细节。 问题 Given a linked list, swap every two adja...

Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...

【LeetCode】Swap Nodes in Pairs

题目描述:Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)