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. rem,em,与px的比较用法

    在Web中使用什么单位来定义页面的字体大小,至今天为止都还在激烈的争论着,有人说PX做为单位好,有人说EM优点多,还有人在说百分比方便,以至于出现了CSS Font-Size: em vs. px v ...

  2. 使用 ReSharper,输入即遵循 StyleCop 的代码格式化规范

    使用 ReSharper,输入即遵循 StyleCop 的代码格式化规范 StyleCop 可以帮助强制执行代码格式化规范,ReSharper 可以帮助你更高效地编写代码.把两者结合起来,你便能高效地 ...

  3. Mysql主外键

    主键  primary   key 创建表时直接加上主键: create table student1(id int primary key, name varchar(20), age int, g ...

  4. [双系统linux] ----安装完成后无法打开wifi

    在安装了linux 双系统以后发现无法打开wifi和蓝牙. rfkill list all 0:ideapad_wlan: Wireless LANSoft blocked: noHard block ...

  5. C++字节对齐与位域

    环境: win7_x64旗舰版.VS2015企业版 一.字节对齐: 说明:为了提高 CPU 的存储速度,编译器会对 struct 和 union的存储进行优化,即进行字节对齐. 1. 指定对齐参数值: ...

  6. springboot2.0 springcloud 断路器仪表盘支持

    springboot 1.5 的时候  springcloud 添加 断路器仪表盘  按照网上的方法是没有问题的  但是 springboot2.0的时候一直无法连接 所以需要添加 @Beanpubl ...

  7. oracle 中update多个字段

    update  A set (A.a2,A.a3) =(select B.b2,b.b3 from  B where B.b1= A.a1 and A.a3=100 )

  8. 分布式事务(二)Java事务API(JTA)规范

    一.引子 既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器.数据库/mq等厂商使用,以方便管理互通--->JTA闪亮登场.JTA(Java Transact ...

  9. Eclipse从GitHub下载代码

    转载自:http://blog.csdn.net/u014785687/article/details/73473769 打开git视图(window->show view),搜索git,选择G ...

  10. PCIE读书笔记

    PCIE读书笔记 什么是TLP: