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.

Subscribe to see which companies asked this question

 
未优化的代码,用3个临时指针
ListNode* swapPairs(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode *pre = head;
ListNode *cur = pre->next;
ListNode *net = cur->next;
head = cur;
while (true)
{
cur->next = pre;
if (net == nullptr || net->next == nullptr) {
pre->next = net;
break;
}
pre->next = net->next;
pre = net;
cur = pre->next;
net = cur->next;
}
return head;
}

惊人的解法,用双重指针,开阔思路,本以为自己可以随心所欲使用双指针,看来还是差得远

ListNode *swapPairs(ListNode *head) {
ListNode **p = &head; while (*p && (*p)->next) {
ListNode *t = (*p)->next; (*p)->next = t->next;
t->next = *p;
*p = t; p = &(*p)->next->next;
} return head;
}

Swap Nodes in Pairs leetcode的更多相关文章

  1. Swap Nodes in Pairs——LeetCode

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

  2. Swap Nodes in Pairs leetcode java

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

  3. Swap Nodes in Pairs LeetCode题解

    做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...

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

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

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

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

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

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

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

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

  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. Ubuntu 安装wireshark

    参考:ubuntu下安装wireshark 依赖及相关包的安装 1.编译工具 apt-get install build-essential 2.GTK+的开发文件和GLib库(libraries) ...

  2. js判断MAC地址

    function white_mac_FormCheck(mac)    {           var temp = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0- ...

  3. jQuery event,冒泡,默认事件用法

    jQuery event,冒泡,默认事件用法 <%@ page language="java" import="java.util.*" pageEnco ...

  4. Android中的IPC机制

    Android IPC简介 IPC是Inter-Process Communication的缩写,含义就是进程间通信或者跨进程通信,是指两个进程之间进行数据交换的过程.那么什么是进程,什么是线程,进程 ...

  5. 图片上传之FileAPI与NodeJs

    HTML5之fileAPI HTML5之fileAPI使得我们处理图片上传更加简单. 实例 html代码 <div class="form-group"> <la ...

  6. Hadoop权威指南:数据完整性

    Hadoop权威指南:数据完整性 [TOC] 常用的错误检测码是CRC-32(循环冗余校验) HDFS的数据完整性 HDFS会对写入的所有数据计算校验和,并在读取数据时验证校验和 datanode负责 ...

  7. ABP入门系列(10)——扩展AbpSession

    ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 一.AbpSession是Session吗? 1.首先来看看它们分别对应的类型是什么? 查看源码发 ...

  8. ArcGIS Pro 简明教程(3)数据编辑

    ArcGIS Pro 简明教程(3)数据编辑 by 李远祥 数据编辑是GIS中最常用的功能之一,ArcGIS Pro在GIS数据编辑上使用习惯有一定的改变,因此,本章可以重点看看一些编辑工具的使用和使 ...

  9. Picasso 修改缓存路径

    Picasso 是 Square 公司开源的一个非常友好的图片加载框架,使用范围也比较广泛.具体的使用这里就不做介绍了,文章主要讲讲如何修改图片的缓存路径.Picasso默认的缓存路径位于data/d ...

  10. 智能指针shared_ptr

    // 智能指针会自动释放所指向的对象. // shared_ptr的应用场景是:程序需要在多个对象间共享数据 /* 先从应用场景入手吧,说矿工A发现了一个金矿. * 然后矿工A喊来了矿工B,一起开采, ...