题目:

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. Zookeeper总概

    zookeeper是一个开源的分布式协调服务.是典型的分布式数据一致性的解决方案. zookeeper可以保证以下分布式一致性的特性 1. 顺序性:同一客户端发起的事务请求,最终会严格的按照发出顺序应 ...

  2. Java中Excel导入功能实现、excel导入公共方法_POI -

    这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> ...

  3. 基于CAS实现单点登录(SSO):工作原理

    工作中使用到了SSO,网上看到了这个博客的一系列文章感觉不错,转载收藏 源地址http://blog.csdn.net/tch918/article/details/19930037 系列文章的第一篇 ...

  4. Erlang 集群互连测试

    Erlang 集群互连测试Erlang节点相同cookie全互联成为一个集群(cluster).如果2个集群不同cookie, 然后其中有节点连接到对方集群的节点,这2个集群会合并成一个集群吗?连接到 ...

  5. Android存储之SharedPreferences

    Android数据存储之SharedPreferences SharedPreferences对象初始化 SharedPreferences mSharedPreferences = getShare ...

  6. (九十)使用多个storyboard+代码实现控制器的分开管理

    使用单个storyboard会使得项目难与管理,使用纯代码又会过于麻烦,因此如果能将二者结合起来,并且使用多个storyboard,会使得项目简单简单.方便许多. 下面以一个简单的视图关系为例,介绍多 ...

  7. Mybatis执行BaseExecutor(二)

    BaseExecutor是Executor的一个子类,是一个抽象类,其实现了接口Executor的部分方法,并提供了三个抽象方法doUpdate.doFlushStatements和doQuery在他 ...

  8. 5个你不知道的HTML5的接口

    原文地址:5 HTML5 APIs You Didn't Know Existed 原文日期: 2010年09月27日 翻译日期: 2013年8月7日 当人们看到或者说出"HTML5&quo ...

  9. SIM900A设备在保加利亚无法正常使用GPRS问题

    1.      SIM900A设备在保加利亚GPRS功能无法正常使用 我们一款手持设备采用SIM900A模块,在发货之前测试正常,但到了保加利亚,客户发现无法正常上网,我们技术支持反馈的邮件反馈的现象 ...

  10. Java中函数的递归调用

    说到递归,java中的递归和C语言中也是很相似的,在Java中,递归其实就是利用了栈的先进后出的机制来描述的. public class HelloWorld { public static void ...