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. C#与Java互通AES算法加密解密

    /// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...

  2. MySQL密码丢失,解决方法

    我的MySQ安装路径是:D:\Program Files\MySQL 1.所以先cmd下切入盘 输入-> D: 输入->cd "D:\Program Files\MySQL\My ...

  3. JavaWeb学习篇之----HTTP协议详解

    简介: HTTP是hypertexttransfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程. HTTP协 ...

  4. jackson - 生成jason工具-简单示例

    主页: http://jackson.codehaus.org/ https://github.com/FasterXML/jackson 当前jackson分为三部分,需要分别下载: jackson ...

  5. php之 人员的权限管理(RBAC)

    1.想好权限管理的作用? 2.有什么权限内容? 3.既然有权限管理那么就会有管理员? 4.登录后每个人员的界面会是不一样的? 一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户 ...

  6. 创建 OVS vlan101 并部署 instance - 每天5分钟玩转 OpenStack(139)

    前面我们创建了 OVS vlan100 并部署了 instance,今天继续创建 vlan101. subnet IP 地址为 172.16.101.0/24. 底层网络发生了什么变化 Neutron ...

  7. android 项目更改包名的方法

    本文章全文转载: http://www.2cto.com/kf/201304/206747.html 1.在项目上右键,选择android tools->rename application p ...

  8. 如何解决在chrome中自动完成表单后input出现黄色背景

    可以对input:-webkit-autofill使用足够大的纯色内阴影来覆盖input输入框的黄色背景:如: 代码如下: input:-webkit-autofill { -webkit-box-s ...

  9. D. Jzzhu and Numbers

    这就是这个题目的意思,真的感觉这个思想是太神奇了,我这种菜逼现在绝壁想不到这样的证明的过程的,还有就是这个题的推道过程,以下思路纯属借鉴卿学姐的,还是自己太菜了,,,, 讲道理这种问题我真的想不到用容 ...

  10. css3瀑布流

    css3虽然可以实现,不过要是真的运用到项目中还是老老实实写js吧 .container{ /*列的宽度*/ column-width:160px; -webkit-column-width:160p ...