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



相关文章推荐

算法设计与分析(17)-- Swap Nodes in Pairs(难度:Medium)

Swap Nodes in Pairs

LeetCode Swap Nodes in Pairs

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

LeetCode之24----Swap Nodes in Pairs

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

[leetcode: Python]24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->...
  • NXHYD
  • NXHYD
  • 2017年05月19日 15:37
  • 194

leetcode-24 Swap Nodes in Pairs

问题描述地址:https://leetcode.com/problems/swap-nodes-in-pairs/ 描述: Given a linked list, swap every two...

[24] Swap Nodes in Pairs

交换一个单项链表中相邻的元素,且不使用多余的空间,只操作单向链表中的节点指针。...
  • camlot_
  • camlot_
  • 2016年06月28日 13:21
  • 159

Swap Nodes in Pairs (Java)

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->3-...
  • wyc1230
  • wyc1230
  • 2012年02月16日 12:36
  • 1517

Swap Nodes in Pairs(LintCode)

题目来源:LintCode 原题地址:http://www.lintcode.com/zh-cn/problem/swap-nodes-in-pairs/ 题目: Given a linke...
  • lyy_hit
  • lyy_hit
  • 2015年10月25日 13:55
  • 356

LeetCode OJ-24-Swap Nodes in Pairs

题目:Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Swap Nodes in Pairs
举报原因:
原因补充:

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