Swap Nodes in Pairs

描述

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

示例:

给定 1->2->3->4, 你应该返回 2->1->4->3.

说明:

  • 你的算法只能使用常数的额外空间。

  • 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

思路

该题属于基本的链表操作题。

  • 设置一个虚拟头结点dummyHead

  • 设置需要交换的两个节点分别为node1、node2,同时设置node2的下一个节点next

在这一轮操作中
  • 将node2节点的next设置为node1节点

  • 将node1节点的next设置为next节点

  • 将dummyHead节点的next设置为node2

  • 结束本轮操作

接下来的每轮操作都按照上述进行。

代码

public static ListNode swapPairs(ListNode listNode) {
if (listNode == null) {
return null;
}
ListNode head = new ListNode(-1);
head.next = listNode;
ListNode pHead = head;
while (pHead.next != null && pHead.next.next != null) {
ListNode node1 = pHead.next;
ListNode node2 = pHead.next.next;
ListNode next = node2.next; node2.next = node1;
node1.next = next;
pHead.next = node2; pHead = node1;
}
return head.next;
}

[LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)的更多相关文章

  1. leetcode 24. Swap Nodes in Pairs(链表)

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

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

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

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

  4. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

  5. (链表 递归) leetcode 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  6. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  7. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

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

  8. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  9. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

随机推荐

  1. Qt编写自定义控件44-天气仪表盘

    一.前言 天气仪表盘控件是所有控件中唯一一个使用了svg矢量图的控件,各种天气图标采用的矢量图,颜色变换采用动态载入svg的内容更改生成的,其实也可以采用图形字体来做,本次控件为了熟悉下svg在Qt中 ...

  2. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  3. C#登录SSH执行命令,下载文件

    前言 批量登录SSH执行命令 ,把应急响应中的日志文件下载回来. 代码实现 Renci.SshNet编译出DLL,引用. using System; using System.Collections. ...

  4. matlab截取图像

    声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 对于Matlab的使用情况常常是这样子的,很多零碎的函数名字很难记忆,经常用过后过一段时间就又忘记了,又得去网 ...

  5. python2.7 升级到 python3.6

    1.命令 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel t ...

  6. 微信公众号 微信连wifi最全的教程

    微信公众号 微信连wifi最全的教程 http://jingyan.baidu.com/article/1709ad806c69bd4634c4f0b3.html 放到自定义菜单 只能用新建图文 把二 ...

  7. electron node.js 实现文件拖动读取文件

    css/styles.css .for_file_drop { width: 100%; height: 100px; background-color: blueviolet; } index.ht ...

  8. 转:TSDF in Kinect fusion

    KinectFusion中用到的TSDF Fusion 原po:https://blog.csdn.net/qq_31785865/article/details/78524429 最近在看关于稠密三 ...

  9. ROS学习(二)运行keyboard

    1.ssh连接机器人 ./dora*.sh 启动roscore,相当于启动ros系统 roscore 2.另启终端,也就意味着重新ssh连接机器人, ./dora*.sh 运行wheel驱动 rosr ...

  10. [ReferenceError: __insane_exports is not defined] [monaco-editor@0.18.0] [vue] [typescript]

    npm install monaco-editor@ 安装上面的命令,安装0.17.0版本.