本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. 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 ...

  2. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  4. 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 ...

  5. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  6. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  7. 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 ...

  8. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  9. 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 ...

随机推荐

  1. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

  2. Dynamics CRM2013 Form利用window.location.reload()进行全局刷新带来的问题及解决办法

    CRM2013以后,表单的保存后变成了局部刷新而非全局刷新,但很多情况下我们需要刷新整个页面,通过刷新页面来使脚本执行或者业务规则执行来实现某些业务效果,一般我们会使用window.location. ...

  3. 基于Web在线考试系统的设计与实现

    这是一个课程设计的文档,源码及文档数据库我都修改过了,貌似这里复制过来的时候图片不能贴出,下载地址:http://download.csdn.net/detail/sdksdk0/9361973   ...

  4. studio中碰到的jni问题:java.lang.UnsatisfiedLinkError

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52606328 studio中碰到 ...

  5. 指令汇B新闻客户端开发(六) 浅谈屏幕适配解决方案

    屏幕适配的问题,我相信很多大牛的经验远比我丰富,在此就简单的分享一下我所做的的屏幕适配方案,当然我说的是安卓方面的啦,嘿嘿,屏幕适配我们一般用1280*720的屏幕作为我们的主流开发屏,当然现在And ...

  6. 【Netty源码学习】ChannelPipeline(一)

    ChannelPipeline类似于一个管道,管道中存放的是一系列对读取数据进行业务操作的ChannelHandler. 1.ChannelPipeline的结构图: 在之前的博客[Netty源码学习 ...

  7. FFmpeg源代码结构图 - 编码

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  8. Android之EditText imeOptions属性解析

    在我们的手机中,虽然通常输入法软键盘右下角会是回车按键,但我们经常会看到点击不同的编辑框,输入法软键盘右下角会有不同的图标.例如:  点击浏览器网址栏的时候,输入法软键盘右下角会变成"GO& ...

  9. UNIX网络编程——select函数的并发限制和 poll 函数应用举例

    一.用select实现的并发服务器,能达到的并发数,受两方面限制 1.一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n来调整或者使用setrlimit函数设置,  ...

  10. JavaScript介绍-javaScript学习之旅(一)

    javaScript简介 1.javaScript是互联网上最流行的脚本语言,这门可用于web和html,更可广泛用于服务器端,pc端,移动端. 2.javaScript脚本语言: javaScrip ...