本文是在学习中的总结,欢迎转载但请注明出处: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. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  2. 微信小程序基础之常用控件text、icon、progress、button、navigator

    今天展示一下基础控件的学习开发,希望对大家有所帮助,转载请说明~ 首先延续之前的首页界面展示,几个跳转navigator的使用,然后是各功能模块的功能使用 一.text展示 使用按钮,进行文字的添加与 ...

  3. 剑指Offer——知识点储备-设计模式

    剑指Offer--知识点储备-设计模式 设计模式 设计模式的六大原则 (1)单一职责原则(有且仅有一个原因引起类的变化): (2)里氏替换(任何父类出现的地方子类都可以替换): (3)依赖倒置(依赖抽 ...

  4. springMVC源码分析--HandlerAdapter(一)

    HandlerAdapter的功能实际就是执行我们的具体的Controller.Servlet或者HttpRequestHandler中的方法. 类结构如下:

  5. App安全(一) Android防止升级过程被劫持和换包

    文/ Tamic 地址/ http://blog.csdn.net/sk719887916/article/details/52233112 前言 APP 安全一直是开发者头痛的事情,越来越多的安全漏 ...

  6. RE模块疑问

    待处理: >>> re.findall(r'[-+]*\d+(?:\.\d+)?','-++++---+123.012') ['-++++---+123.012'] >> ...

  7. YCSB性能测试工具使用

    在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为"Yahoo! Cloud Serving Benchmark".它内置了对常见 ...

  8. collection 中对类排序

    首先 写出 一个person类 让他继承Comparable 构造函数和get/set不用说 我们要覆盖父类中的comparto方法 代码如下 省略get/set package a; public ...

  9. 有n个数,输出其中所有和为s的k个数的组合。

    分析:此题有两个坑,一是这里的n个数是任意给定的,不一定是:1,2,3...n,所以可能有重复的数(如果有重复的数怎么处理?):二是不要求你输出所有和为s的全部组合,而只要求输出和为s的k个数的组合. ...

  10. Cocos2D:变换(transforms)在图形编程中扮演的角色

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...