本文是在学习中的总结,欢迎转载但请注明出处: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. vuejs关于函数式组件的探究

    所以,在控制台里app1.exist 或app2.exist都可以控制是否显示字母. <!DOCTYPE html> <html lang='zh'> <head> ...

  2. 用Netty解析Redis网络协议

    用Netty解析Redis网络协议 根据Redis官方文档的介绍,学习了一下Redis网络通信协议.然后偶然在GitHub上发现了个用Netty实现的Redis服务器,很有趣,于是就动手实现了一下! ...

  3. JVM:类的生命周期

    类的生命周期 综述 1.    只有当一个类被切实使用到的时候才会被加载到虚拟机中(例如:new, 方法调用, A a = null;不算) 2.    若在加载一个类的过程中,有其他类被切实使用到, ...

  4. 计算机网络之IP地址

    IP地址的分类 整个的因特网就是一个单一的.抽象的网络.IP地址就是给因特网上的每一个主机(或路由器)的每一个接口分配一个在全世界范围内唯一的32位的标识符. 所谓分类的IP地址,就是将IP地址划分为 ...

  5. 独立开发一个云(PaaS)的核心要素, Go, Go, Go!!!

    最近一年的工作,有很大的比重在做云平台的事情,简单来说,就是为公司内用户提供一个PaaS,用户可以在我们的云平台上方便的将单机服务程序扩展为多实例程序,以平台服务化的方式对外提供.在这里简单分享一下. ...

  6. 好久没有写BLOG了,人老了就开始变懒了【非技术】

    算算到今天,在码农的路上已经走了15年了.马上就40不惑的我,现在是充满的疑惑.

  7. iOS学习笔记--数据存储

    iOS应用数据存储的常用方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data 1. XM ...

  8. x264源代码简单分析:x264命令行工具(x264.exe)

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  9. java.util.ServiceLoader使用

    近期在项目中需要实现能在配置文件中定义多个统一接口类型的类,可以在程序中获取到所有配置的类,刚开始打算配置到properties中,然后去程序读取,感觉这种方式不太灵活,于是,研究研究java中有没有 ...

  10. 从嵌入式linux到android应用开发

      时间过得很快,转眼之间已经到新公司一个月了.虽然学到了一些移动开发的知识,但是觉得离我的目标还很远,完全没能达到我想要的水平.以前产品都是自己主导的,需要完成什么,计划什么也是自己主导,现在得从头 ...