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. 在VMware上安装CentOS6 64位操作系统

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

  2. auto-encoder小记

    1.使用auto-encoder生成手写数字 2.中间code层使用二维向量,使用L2norm处理中间层数据 3.从[-1,1]的矩形框中等间隔选取100个坐标点 作为code值 最终生成图像 后期应 ...

  3. C语言几个输入函数的区别(史上最详细)

    The difference of the string and the character(char): 字符串是一个带有""的字符序列如 "I fuck xuqian ...

  4. 将TUM数据集的RGB-D数据集转化为klg格式

    1.在github上下载代码png_to_klg git clone https://github.com/HTLife/png_to_klg 2.将png_to_klg目录下的associate.p ...

  5. TCP 三次握爪 四次挥手

    TCP三次握手和四次挥手过程 1.三次握手 (1)三次握手的详述 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源.Client端接收到ACK报文后也向 ...

  6. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错

    有些时候,<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错,错 ...

  7. Google - Find minimum number of coins that make a given value

    Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { ...

  8. 在Django中运行ExtJS 事例

    网上关于ExtJS的事例挺多的,但是在Django中使用ExtJS挺少的,当然了,一些大牛觉得ExtJS运用在页面上是很简单的事,但是对于菜鸟来说,实在有点困难. 我这个例子是用在了sublime3这 ...

  9. vue原生table合并单元格并可编辑

    <template> <div> <div class="el-card box-card table_container"> <div ...

  10. 2018-2019-2 20165308网络对抗技术 Exp6:信息收集与漏洞扫描

    2018-2019-2 20165308网络对抗技术 Exp6:信息收集与漏洞扫描 实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册 ...