Leetcode_24_Swap Nodes in Pairs
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43302355
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.
思路:
(1)题意为给定一个链表,交换链表中相邻元素的位置。
(2)该题主要考察当链表中节点位置发生变化时,如何对链表进行更新的问题。
(3)本文的方法是:首先,创建一个新的节点result,用以保存交换后的链表,称之为结果链表;创建两个标志flag1和flag2,分别记录结果链表最后一个节点和交换的两个节点的下一个节点。然后,设置节点curr指向当前head,只要curr和curr的下一个节点不为空,就循环遍历,在遍历的过程中,第一次遍历需要确定result所对链表的头结点,这里,result指向curr.next,即链表的第二节点,flag2指向curr.next.next,即第三个节点,因为在交换第一个和第二个节点后,需要知道下一个待交换的节点,这里需要把第三个节点保存起来;原先的第一个节点指向了第二个节点,交换后变为第二个节点指向第一个节点,即第一个节点curr.next.next=curr;而此时,flag1需要保存结果链表的最有一个节点,以备在后续交换中直接将其它节点加在其后面,所以flag指向curr,即交换后,第一个节点变为了第二个节点;交换完成后,curr继续向后移动,进行后续节点的交换。最后,循环操作,直到所有节点参与交换,所得result即为结果。
(4)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public class Swap_Nodes_in_Pairs {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode curr = head;
ListNode flag1 = null;
ListNode result = null;
ListNode falg2 = null;
while (curr != null && curr.next != null) {
if (result == null) {
result = curr.next;
falg2 = curr.next.next;
curr.next.next = curr;
curr.next = falg2;
flag1 = curr;
curr = curr.next;
} else {
falg2 = curr.next.next;
flag1.next = curr.next;
flag1.next.next = curr;
curr.next = falg2;
flag1 = curr;
curr = curr.next;
}
}
return result;
}
Leetcode_24_Swap Nodes in Pairs的更多相关文章
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...
随机推荐
- Redis源码学习:Lua脚本
Redis源码学习:Lua脚本 1.Sublime Text配置 我是在Win7下,用Sublime Text + Cygwin开发的,配置方法请参考<Sublime Text 3下C/C++开 ...
- PGM:图模型学习概述
http://blog.csdn.net/pipisorry/article/details/52571640 动机 前面我们讨论的问题出发点是给定一个图模型.如在独立性和推理讨论中,假定模型--结构 ...
- ELK搭建
ELK安装 elasticsearch安装 * 下载elasticsearch-5.0.0.tar.gz,并解压. 通过elasticsearch.yml可设置host和port. vim confi ...
- Android实现多条Toast快速显示(强制中止上一条Toast的显示)
Android实现多条Toast快速显示 Toast多用于我们开发人员调试使用,有时候也作为给用户的弱提示使用,我们常用的方法是 Toast.makeText(this, "弹出Toast& ...
- EBS业务学习之应收管理
Oracle Receivable 是功能完备地应收款管理系统,它能够有效地管理客户.发票和收帐过程,因此是财务模块的重要组成部分,是财务系统中较为核心的模块之一.对于一个公司来说,是否能够与客户保持 ...
- 使用shell操作HDFS
前提是都已经配置好了,可以参考hadoop伪分布安装:http://blog.csdn.net/jerome_s/article/details/25788967 linux的文件系统与hdfs的关系 ...
- 在Gazebo中使用DEM構建起伏地形環境
所需資料下載地址: 1. https://bitbucket.org/osrf/gazebo_tutorials/raw/default/dem/files/ 数字高程模型(致謝谷歌翻譯)概述数字高程 ...
- SSO 基于CAS实现单点登录 实例解析(二)
本文目录: 概述 演示环境 部署CAS-Server相关的Tomcat 部署CAS-Client相关的Tomcat 测试验证SSO 第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登 ...
- Vibrator控制手机震动
Vibrator控制手机震动 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755 添加权限 & ...
- Android Paint类介绍以及浮雕和阴影效果的设置
Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制信息. 1.图形绘制 setARGB(int a,int r,int g,int b); 设置绘制的颜色,a代表透明 ...