题目:

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.

思路:

  • 首先是判断head和head.next为空的情况,直接返回head
  • 然后设置first和second两个变量,用来记录一前一后两个元素
  • 设置pre来记录上一次最后的元素。pre.next = second;
  • -

代码:

/**
 * 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 first = head;
        ListNode second = head.next;
        ListNode pre = null;
        ListNode swap = null;
        if(second != null){
            head = second;
        }
        while(first != null&&second != null){
            swap = second.next;
            second.next = first;
            first.next = swap;
            if(pre != null){
                pre.next = second;
            }
            pre = first;
            first = swap;
            if(swap != null){
                second = swap.next;
            }
        }
        return head;
    }
}

LeetCode之旅(21)-Swap Nodes in Pairs的更多相关文章

  1. leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现

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

  2. Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array

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

  3. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  4. LeetCode(24) Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  5. 【leetcode❤python】24. Swap Nodes in Pairs

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  6. leetcode个人题解——#24 Swap Nodes in Pairs

    因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...

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

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

  8. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  9. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

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

随机推荐

  1. 第一行代码阅读笔记----显示隐式Intent的基本用法

    1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...

  2. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  3. Android Studio设置代理更新下载SDK

    代理主机和端口号按下图设置即可,便可以轻松的下载更新SDK啦~~~

  4. unix os下du df简单用法

    转自:http://dadoneo.iteye.com/blog/984963 du命令参数详解见:http://baike.baidu.com/view/43913.htm 下面我们只对其做简单介绍 ...

  5. 1、MyEclipse插件配置以及通过MyEclipse生成表对应的JPA代码

     去除MyEclipse插件的方式是打开:WindowàCustomize Perspective窗口进行插件配置: 取出下图中不常用的插件勾,最终点击OK. 3.点击OK之后显示的效果图如下: ...

  6. Swift基础用法(Swift开发之一)

    昨晚苹果发布了新一代编程语言Swift,官方提供了一个iBook的说明文档,有需要的可以看下.地址:https://itunes.apple.com/cn/book/swift-programming ...

  7. 【shell脚本】ftp自动上传mysql备份文件

    上一篇中 mysql每日备份shell脚本 给出了使用mysqldump备份到本地的脚本,接着下面是利用ftp把备份文件传输到远程服务器的脚本. 当然也可以用scp,rsync等等方案. #!/bin ...

  8. zookeeper学习总结

    最近一两天,一直在看zookeeper,自己也感觉头昏脑涨的. 现记录一下,最近的所得: 安装与配置: http://blog.csdn.net/morning99/article/details/4 ...

  9. Mybatis接口编程原理分析(一)

    Mybatis接口编程示例 (1)首先定义接口编程需要的接口及其方法 public interface IUserMapper { public User getById(int id);//接口方法 ...

  10. 怎样写一个与Windows10 IE11兼容的标准BHO?

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...