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 ...
随机推荐
- 【java集合系列】--- LinkedList
开篇前言--LinkedList中的基本用法 在前面的博文中,小编介绍List接口中的ArrayList集合,List这个接口,有两个实现类,一个就是ArrayList另一个是LinkedList(链 ...
- mongo 读分析
分布式读 读冲突 分布式中数据库有多份数据,各份数据可能存在不一致性. mongo 只会写到primary节点上,理论上来说不会有文档冲突,也就是说数据库中的数据都以primary节点为标准. 但是有 ...
- [Boost] 1.57.0 with VS2013 + Intel compiler
The compiled version can be found below. Do not foget to give me a star. :) http://pan.baidu.com/s/1 ...
- 剑指Offer——知识点储备-操作系统基础
剑指Offer--知识点储备-操作系统基础 操作系统 操作系统什么情况下会出现死锁? 产生死锁的必要条件 (1)互斥条件:即某个资源在一段时间内只能由一个进程占有,不能同时被两个或两个以上的进程占有, ...
- RxJava(九)zip操作符在Android中的实际使用场景
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51614927 本文出自:[余志强的博客] 一.zip操作符概述 官方 ...
- C++ ifstream,ofstream读写二进制文件
为什要吧数据存为二进制 这个嘛,是我个人习惯,一般,我们会把日志文件存为文本文件.数据文件存成二进制文件. 其实,我们接触的文件,比如图像.视频都是以二进制的形式存储的,要想查看这类数据,必须知道数据 ...
- Java 单元测试 JUnit4 快速入门
JUnit最佳实践 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class CardServiceTest { /** * 最佳 ...
- Vibrator控制手机震动
Vibrator控制手机震动 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755 添加权限 & ...
- introduction of velocity
一.velocity 简介 基于java 的模板引擎,apache 旗下的开源软件项目. 目的在于隔离 表示层和业务逻辑层,当然现在做的不仅仅是这些. 二.应用场景 web 应用程序:创建html页面 ...
- 【NPR】铅笔画
写在前面 今天打算写一篇跟Unity基本无关的文章.起因是我上个星期不知怎么的搜到了一个网站 ,里面实现的效果感觉挺好的,后来发现是2012年的NPAR会议的最佳论文.看了下文章,觉得不是很难,就想着 ...