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. Calling Circles(UVa 247)(Floyd 算法)

    用Floyd算法求出传递闭包,然后用dfs求出每条连通分量.注意其中用到的几个小技巧: #include<cstdio> #include<iostream> #include ...

  2. 在VMware上安装CentOS6 64位操作系统

    ---恢复内容开始--- 1.创建新的虚拟机 2.选择自定义,点击下一步: 3.找到镜像位置,添加: 4.点击“稍后安装操作系统”,点击“下一步”: 5.默认点击“下一步”,然后分配CPU: 这里内存 ...

  3. Uva10562——Undraw the Trees

    上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...

  4. JavaScript属性(第三天)

    js语法非常灵活,这致使他非常好用,也造成它比较难掌握的地方: js中的值类型与引用类型在这里不做过多介绍,可以参照其他语言. js是可以动态添加属性的: var person={}; person. ...

  5. ubuntu 终端作死体验

    [参考]: https://blog.csdn.net/m0_37192554/article/details/81697791 https://blog.csdn.net/amazingren/ar ...

  6. vue重温之mint-ui------------点击事件绑定

    第一次接触mint-ui,在使用它的tabbar时,为了达到点击切换页面的效果,采用了点击事件的方式,然后就碰到了这么个问题: 是的,点击事件是不起作用的. 然后刚好一个朋友也在做这块,而且有一段时间 ...

  7. 关于 legend_noa

    真名:qlw 性别:男 常用ID:legend_noa(有时候也用fseject以表示我的弱, 曾经不懂事用goddess_Q),具体意思是我最喜欢的两个奥特曼:诺亚和雷杰多 p1 诺亚,p2 雷杰多 ...

  8. 巴黎游戏周: PS4独占游戏《重力少女2》

    http://blog.us.playstation.com/2015/10/27/gravity-rush-2-coming-to-north-america-on-ps4/

  9. jquery 蔚蓝网

    $(document).ready(function(){ //提交表单 $("#registerBtn").click(function(){ var email=documen ...

  10. java8_api_nio

    NIO-1    nio的概念    Buffer的属性    Buffer中数据的读写        用以提高IO处理数据的性能问题,之前io里的单位是Byte(java程序向流中写入byte或相反 ...