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. nginx负载均衡分配策略有哪些?

    nginx负载均衡分配策略有哪些?   答: 1.轮询(默认,不用在upstream中配置)方式 2.weight(权重) 当指定的服务器的权重参数,权重占比为负载均衡决定的一部分.权重大负载就大. ...

  2. 性能测试-Linux资源监控⽅式

    Linux资源监控⽅式 1. 命令 2. 第三⽅⼯具(nmon) 3. LR(需要安装RPC相应服务包和开启服务)(略)   ⼀.命令 ⽅式 1. top (系统资源管理器) 2. vmstat (查 ...

  3. python 类型注解

    函数定义的弊端 python 是动态语言,变量随时可以被赋值,且能赋值为不同类型 python 不是静态编译型语言,变量类型是在运行器决定的 动态语言很灵活,但是这种特性也是弊端 def add(x, ...

  4. python判断命令执行成功

    if os.system('lss') !=0: print 'Without the command'

  5. node节点扩容

    node节点扩容: 安装docker 部署 kubelet组件 拷贝admin.conf 拷贝bootstrap.conf 安装cni网络插件 拷贝ca.pem 证书,配置kubelet-config ...

  6. Python3之定制类

    看到类似的__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的 Python中还有许多有特殊用途的函数,可以帮助我们定制类 __str__ 先定义一个S ...

  7. 物联网防火墙himqtt源码之MQTT协议分析

    物联网防火墙himqtt源码之MQTT协议分析 himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Application FireWall,C语言编写,采用epoll模式支持数十万 ...

  8. php open_basedir的使用与性能分析

    php open_basedir的使用与性能分析 使用方法 <pre>/*限制打开的目录*/ini_set('open_basedir', __DIR__.'/');</pre> ...

  9. 1、4 前后端分离,写静态HTML文件,通过ajax 返回数据

    1.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  10. 关于AES加密,以及各种分组加密

    http://blog.csdn.net/searchsun/article/details/2516191