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. mininet的学习之二-----miniedit可视化

    安装ryu git clone git://github.com/osrg/ryu.git cd ./ryu  sudo python setup.py install mininet可视化 git ...

  2. centos7使用cronolog分割tomcat8.5的catalina.out日志

    1.安装cronolog wget https://files.cnblogs.com/files/crazyzero/cronolog-1.6.2.tar.gz tar -zxvf cronolog ...

  3. 使用CNN生成图像先验,实现更广泛场景的盲图像去模糊

    现有的最优方法在文本.人脸以及低光照图像上的盲图像去模糊效果并不佳,主要受限于图像先验的手工设计属性.本文研究者将图像先验表示为二值分类器,训练 CNN 来分类模糊和清晰图像.实验表明,该图像先验比目 ...

  4. Centos下查看当前目录大小及文件个数

    查看目录及其包含的文件的大小 du -ch directory 查看当前目录下文件的个数 ls -l | grep "^-" | wc -l 查看当前目录下以.jpg为后缀文件的个 ...

  5. Vue通过路由 query传递参数

    父组件通过query来传递num参数为1,相当与在 url 地址后面拼接参数 <template> <div> <h3>首页</h3> <rout ...

  6. 思科模拟器-DHCP配置

    一.如图做好以下拓扑图(下图我取消显示端口号) 二.配置最下面的四台上网终端为自动获取ip 三.配置中间的二层交换机:添加vlan,同时所有端口设为access类型并将其加入vlan中,以下以swit ...

  7. day02 运算符

                                  运算符2019-04-01 目录 一.算数运算符 + = * / % // ** 二.比较运算 > < == != >= ...

  8. [IDEA]IDEA设置注释模板

    IDEA的注释模板有类注释模板和方法注释模板两种,下面分别介绍: 一.类注释模板 菜单路径:File->Settings->Editor->File and Code Templat ...

  9. python MySQL执行SQL查询结果返回字典

    写自动化测试的时候我希望执行数据库前置任务,把数据库查询的结果作为请求的参数,但是正常返回结果为列表嵌套里面,这样就会影响到关键字准确的获取,特别的受限于SQL的查询字段的的顺序,所以希望返回的单条数 ...

  10. win10 caffe GPU环境搭建

    一.准备 系统:win10 显卡:gtx1050Ti 前期的一些必要软件安装,包括python3.5.matlab2016.vs2015.git, 可参考:win10+vs2015编译caffe的cp ...