[LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3
.
Note:
- Your algorithm should use only constant extra space.
- You may not modify the values in the list's nodes, only nodes itself may be changed.
要求把相邻的2个节点两两互换,还必须是换指针而不能是只换值
这里我们用递归的方法来处理,两个指针l1和l2,l1-->l2,我们先把l1和l2换了,然后对l1.next.next继续相同的方法递归下去
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head==null || head.next==null) return head;
ListNode res = head.next;
head.next = swapPairs(head.next.next);
res.next = head;
return res;
}
}
[LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- 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 ...
- 24. Swap Nodes in Pairs[M]两两交换链表中的节点
题目 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the va ...
- (链表 递归) 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 ...
- LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...
- [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 ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
随机推荐
- shell-脚本-作用是发现前10个最占用磁盘空间的文件
#!/bin/bash## 这个脚本的作用是发现前10个最占用磁盘空间的文件################定义这个脚本的全区变量##################################C ...
- 详细grep、sed、awk
[root@VM_0_7_centos tmp]# cat 1.txt 1 2 3 4 5 6 [root@VM_0_7_centos tmp]# cat 2.txt 4 5 6 7 8 [root@ ...
- mysql的innodb自增主键为什么不是连续的
图1 图1中是表t原有的数据,这个时候我们执行show create table t会看到如下输出,如图二所示现在的自增值是2,也就是下一个不指定主键值的插入的数据的主键就是2 图2 Innodb引擎 ...
- Python3之XML模块
一.简介 xml是实现不同语言或程序之间进行数据交换的协议,可扩展标记语言,标准通用标记语言的子集.是一种用于标记电子文件使其具有结构性的标记语言.xml格式如下,是通过<>节点来区别数据 ...
- 0.jQuery选择器
左边的是jQuery用法 右边是js用法 $("tag") == document.getElementsByTagName("tag"); $(". ...
- 浅谈C#中的委托、事件与异步
从刚接触c#编程到现在,差不多快有一年的时间了.在学习过程中,有很多地方始终似是而非,直到最近才弄明白. 本文将先介绍用法,后评断功能. 一.委托 基本用法: 1.声明一个委托类型.委托就像是‘类'一 ...
- 20165224 陆艺杰 Exp5 MSF基础应用
用自己的话解释什么是exploit,payload,encode Exploit是攻击的行为 Payload是一段植入目标机的简短的带功能的恶意代码 Encode是编码,用于更改恶意代码,编码特征码检 ...
- 【转】idea中applicationContext-trans.xml中的Cannot resolve bean 'dataSource'...的问题解决
问题如下: (applicationContext-trans.xml中的部分截图) 先了解问题是怎么出现的: 此处的dataSource是在applicationContext-dao.xml中配置 ...
- 教你搭建SpringSecurity3框架(附源码)
源码下载地址:http://pan.baidu.com/s/1qWsgIg0 一.web.xml <?xml version="1.0" encoding="UTF ...
- 【算法笔记】B1002 写出这个数
1002 写出这个数 (20 分)读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 101 ...