Question

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.

Solution

这题的核心在于dummy node和list操作。

dummy -> 1 -> 2 -> 3 -> 4

prev   cur   next  tmp

我们用四个指针来完成操作。

1. 预存tmp: tmp = next.next

2. 更改next: next.next = cur

3. 更改cur: cur.next = tmp

4. 更改prev: prev.next = next

5. 更新prev, cur, next:

cur = tmp

if (cur != null): next = cur.next

prev = prev.next.next

最后返回dummy.next

我们看到循环结束的条件应该是tmp为null或者tmp.next为null.

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy, current = head, next = head.next, tmp;
while (next != null) {
tmp = next.next;
next.next = current;
current.next = tmp;
prev.next = next;
current = tmp;
prev = prev.next.next;
if (current == null) {
break;
} else {
next = current.next;
}
}
return dummy.next;
}
}

Swap Nodes in Pairs 解答的更多相关文章

  1. [LeetCode]Swap Nodes in Pairs题解

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

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

  3. 24. Swap Nodes in Pairs

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

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

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

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

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

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

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

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

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

  9. Leetcode 线性表 Swap Nodes in Pairs

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

随机推荐

  1. javaDay1 基础知识

    常用dos命令 •d: 回车  盘符切换 •dir(directory):列出当前目录下的文件以及文件夹 •md (make directory) : 创建目录 •rd (remove directo ...

  2. 【转】FLV视频封装格式详解

    Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...

  3. Odometer使用JavaScript和CSS制作数字滑动效果

    Odometer是一个使用JavaScript和CSS技术,制作出数字上下滑动的动画效果插件,有点类似与我们的天然气的读数的动画效果,这个插件是轻量级的,压缩版本只有3kg,使用CSS3动画技术,所以 ...

  4. 【公告】CSDN个人空间将于2014年4月20日全新改版上线

    尊敬的用户:   你们好!           CSDN个人空间将在2014年4月20日全新改版上线!        CSDN个人空间是2008年8月推出的服务,致力于给广大用户提供在线技术分享和资料 ...

  5. [转]使用Composer管理PHP依赖关系

    简介 现在软件规模越来越大,PHP项目的开发模式和许多年前已经有了很大变化.记得初学PHP那会儿,boblog是一个很好的例子,几乎可以代表 PHP项目的开发模式.当时PHP 5.x以上的版本刚开始流 ...

  6. 解决android自带textview右侧文字不能对齐的问题

    package com.sixin.view; import android.content.Context; import android.graphics.Canvas; import andro ...

  7. WebApi2官网学习记录---批量处理HTTP Message

    原文:Batching Handler for ASP.NET Web API 自定义实现HttpMessageHandler public class BatchHandler : HttpMess ...

  8. Winform改变Textbox边框颜色(转)

    namespace MyTextBoxOne { //使用时必须把文本框的BorderStyle为FixedSingle才能使用 //一些控件(如TextBox.Button等)是由系统进程绘制,重载 ...

  9. oracle服务的开始和关闭 CMD

    开始: @echo halt oracle service ...net start OracleServiceORCLnet start OracleOraDb11g_home1TNSListene ...

  10. Silverlight Visifire控件 .net后台控制aspx页面控件的显示与隐藏,动态给控件赋值,选定默认值的设定

    .net后台代码: 控件的显示与隐藏: this.dateStart.Visibility = Visibility.Collapsed;//不显示控件 this.dateYear.Visibilit ...