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

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given ->->->, you should return the list as ->->->.

奇数位和偶数位互换,若是奇数个,不用管最后一个

解法一:(C++)

 ListNode* swapPairs(ListNode* head) {
if(!head)
return NULL;
ListNode* dummy=new ListNode(-),*cur=dummy;
dummy->next=head;
while(cur->next&&cur->next->next){
ListNode* t=cur->next->next;
cur->next->next=t->next;
t->next=cur->next;
cur->next=t;
cur=t->next;
}
return dummy->next;
}

java:

 public ListNode swapPairs(ListNode head) {
if(head==null)
return null;
ListNode dummy=new ListNode(-1),pre=dummy;
dummy.next=head;
while(pre.next!=null&&pre.next.next!=null){
ListNode t=pre.next.next;
pre.next.next=t.next;
t.next=pre.next;
pre.next=t;
pre=t.next;
}
return dummy.next;
}

方法二:使用递归的方法,递归遍历到末尾两个,然后交换末尾两个,依次往前遍历(C++)

 ListNode* swapPairs(ListNode* head) {
if(!head||!head->next)
return head;
ListNode* t=head->next;
head->next=swapPairs(head->next->next);
t->next=head;
return t;
}

LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  2. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  5. [LeetCode] Swap Nodes in Pairs 成对交换节点

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

  6. Java [leetcode 24]Swap Nodes in Pairs

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

  7. Leetcode 24——Swap Nodes in Pairs

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

  8. (链表 递归) leetcode 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  9. [leetcode]24. Swap Nodes in Pairs交换节点对

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

随机推荐

  1. 《Java核心技术(卷一)》读书笔记——第六章:内部类

    1.      内部类的概念? 类中类 2.      为什么要用内部类? 内部类的方法可以访问外部类的实例域 内部类对外部类的同一个包中的类实现了隐藏 匿名内部类在“想要定义一个回调函数却又不想编写 ...

  2. asp.net实现伪静态

    一.配置应用程序 1.下载URLRewrite.dll,程序中添加引用 2.在web.config中配置 <configuration> <configSections> &l ...

  3. SQL学习 存储过程&DUAL表

    CREATE OR REPLACE PROCEDURE 存储过程 转自 https://www.cnblogs.com/lideng/p/3427822.html oracle中dual表的用途介绍 ...

  4. 删除排序数组中的重复项-leetcode-26

    public:    int removeDuplicates(vector<int>& nums) {        int size=nums.size();        i ...

  5. cout设置输出数据不显示科学计数法

    [解决方案] 1.在头文件包含—#include<iomanip>——定义IO流输出输入格式控制相关函数. 2.利用cout输出格式为—cout << fixed <&l ...

  6. Java BitSet解决海量数据去重

    先提一个问题,怎么在40亿个整数中找到那个唯一重复的数字? 第一想法就是Set的不可重复性,依次把每个数字放入HashSet中,当放不去进去的时候说明这就是重复的数字,输出这个数字. if(hs.co ...

  7. linux 域名解析

    vi /etc/hosts  中添加ip地址和域名  111.111.111.111   aa.swddjtc.cn   然后重启 /etc/init.d/network restart

  8. [ZZ] MATLAB曲线拟合

    MATLAB曲线拟合 http://blog.sina.com.cn/s/blog_5db2286f0100enlo.html MATLAB软件提供了基本的曲线拟合函数的命令: 多项式函数拟合:  a ...

  9. 环境搭建--使用pytharm远程调试树莓派

    对于Linux和文本编辑器不那么熟悉的小伙伴来说,直接在树莓派中写程序可谓是痛苦万分.本文将介绍如何使用PyCharm远程调试树莓派,并同步当前python文件到树莓派中. 配置环境 首先要在个人电脑 ...

  10. 使用xheditor时 cloneRange错误 ext.net

    使用ext.net 加  xheditor时,一直报 cloneRange错误. 于是 按照说明但独使用xheditor  ,检查无错,正常使用, 因此排除版本问题. <ext:panel ru ...