leetcode24,交换链表相邻的节点
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
对于这样链表交换的题目,我一般都有两种解法。
第一种,看链表每个节点带有的数据量,这道题带有只有一个value值,只有一个int值,所以我一开始的想法就是,不真实的交换节点,只是交换里面的数据。
这样的好处是空间复杂度特别省,适合那些每个节点数据很少,要求不高的链表交换。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null)
return head; ListNode A = head;
ListNode B = head.next; int t;
while(A != null && B != null){
t = B.val;
B.val = A.val;
A.val = t; if(B.next == null)
break;
A = B.next;
B = B.next.next;
} return head;
}
}
第二种解法就是真的去交换节点。
那么就需要多余的节点去记录当前结点的情况然后去交换
我用h,1,2,3这4个节点来说明。
123是原来的节点。
而h是头节点,指向1
下面是交换的步骤。
h->1->2->3
1->3(这个3通过2.next找)
h->2(这个直接有2)
h->->1(这个1也是直接有)
h->3(这个3通过1.next找)

leetcode24,交换链表相邻的节点的更多相关文章
- [Swift]LeetCode24. 两两交换链表中的节点 | Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
- Leetcode24.Swap Nodes in Pairs两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- 算法修炼之路——【链表】Leetcode24 两两交换链表中的节点
题目描述 给定一单链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是简单的改变节点内部的值,而是需要实际的进行节点交换. 示例: 输入:head = [1, 2, 3, 4] 输出:hea ...
- LeetCode24 两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- LeetCode----两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- [LeetCode] 24. 两两交换链表中的节点
题目链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是 ...
- 两两交换链表中的节点(java实现)
题目: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你应该返回 ...
- LeetCode--024--两两交换链表中的节点(java)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你应该返回 2-&g ...
- 【LeetCode题解】24_两两交换链表中的节点(Swap-Nodes-in-Pairs)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度要求) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解 ...
随机推荐
- rune is alias of int32
I think chendesheng's quote gets at the root cause best: Go uses a lot of signed values, not just fo ...
- mac关机快捷键
1.Ctrl + 关机:弹出关机提示 2.Ctrl + Option + 关机 : 正常关机快捷键 3.Command + Option + 关机 :进入休眠状态 4.Ctrl + Command + ...
- myeclipse6.0安装svn插件
myeclipse6.0安装svn插件 转载地址:http://www.cnblogs.com/danica/archive/2011/07/12/2104323.html myeclipse6.0安 ...
- kafka_2.11-0.10.0.0安装步骤
Kafka安装配置 我们使用5台机器搭建Kafka集群: 1. cluster-1-namenode-1-001 172.16.0.147 2. cluster-1-datanode-1- ...
- lr_abort()、exit(-1) 和 return-1之间的区别
int status; status = web_url("Login", "URL=https://secure.computing.com//login.asp?us ...
- fiddler抓包使用①
链接:http://jingyan.baidu.com/article/3a2f7c2e0d5f2126aed61175.html 设置好代理后,有的设备需要访问"192.168.1.1 ...
- geekNews 学习总结
1:下移隐藏,上移出现 //android.support.v4.widget.NestedScrollView nsvScroller; //FrameLayout llDetailBottom; ...
- java学习初体验之课后习题
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { //打印Hel ...
- 设计模式 -- 解释器模式(Interpreter Pattern)
2015年12月15日00:19:02 今天只看了理论和demo,明天再写文章,跑步好累 2015年12月15日21:36:00 解释器模式用来解释预先定义的文法. <大话设计模式>里面这 ...
- Javaweb 第5天 mysql 数据库课程
MySQL数据库课程 两日大纲 ● 数据库的概念.MySQL快速入门.SQL语言简介 ● 数据库操作.表操作.数据记录操作.数据类型和约束 ● 查询 ● 多表关系.多表连接查询 ● 视图 ● 数据备份 ...