Swap Nodes in Pairs

描述

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

示例:

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

说明:

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

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

思路

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

  • 设置一个虚拟头结点dummyHead

  • 设置需要交换的两个节点分别为node1node2,同时设置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. Ubunut16.04 安装 g++ gcc 降级

    1. 查看gcc版本和g++版本 cd /usr/bin ls -l gcc* ls -l g++* 2. 安装gcc和g++ 4.4版本 sudo apt-get install gcc-4.4 g ...

  2. 单层反查BOM

    *&---------------------------------------------------------------------* *& Report YCX_001 * ...

  3. spring-core-5.0.6.RELEASE-sources.jar中java源代码不全

    笔者最近在调试一段代码,进入spring-core以后,IDEA帮我反编译出源码,其中MethodProxy.java如下 // // Source code recreated from a .cl ...

  4. LODOP打印超文本字符串拼接1 固定表格填充数值

    前面的博文:Lodop打印控件传入css样式.看是否传入正确样式.Lodop打印如何隐藏table某一列,Lodop传入的样式可以不是页面本身的css样式,传入什么打印什么,此外,数据也是,超文本打印 ...

  5. ADFS RelayState

    https://blogs.technet.microsoft.com/askds/2012/09/27/ad-fs-2-0-relaystate/ 什么是RelayState,我为什么要关心?有两种 ...

  6. ADFS2016和ADFS代理的安装与配置

    一,ADFS安装教程 教程链接(包含安装和配置两个步骤): https://www.virtuallyboring.com/how-to-setup-microsoft-active-director ...

  7. CEIWEI CheckSum CRC校验精灵v2.1 CRC3/CRC4/CRC5/CRC6/CRC8CRC10/CRC11/CRC16/CRC24/CRC32/CRC40/CRC64/CRC82/Adler32

    CEIWEI CheckSum CRC校验精灵 是一款通用的循环冗余校验码CRC(Cyclic Redundancy Check).MD5.SHA1.SHA2.SHA3.HAVAL.SHAKE.TIG ...

  8. 云计算第一章:服务器硬件及linux初体验

    第一章:服务器硬件及linux初体验 一.了解linux: 1.linux的三大分支:debian.redhat.ubuntu 2.redhat版本: redhat:红帽,简称RHEL,企业级官方版本 ...

  9. vue-cli2.0项目的搭建

    1.第一步:安装vue-cli 命令行输入cmd或按住shift鼠标右键打开powershell 输入命令 npm install --global vue-cli 按enter键 2.第二步:创建项 ...

  10. mysql配置优化的参数

    1.MySQL数据库高效优化解析  Mysql优化是一项非常重要的工作,而且是一项长期的工作,曾经有一个为位DBA前辈说过:mysql的优化,三分配置的优化,七分sql语句的优化. Mysql的优化: ...