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. Django之model详解

    Django中的页面管理后台 Djano中自带admin后台管理模块,可以通过web页面去管理,有点想php-admin,使用步骤: 在项目中models.py 中创建数据库表 class useri ...

  2. 【Leetcode_easy】682. Baseball Game

    problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...

  3. Nginx作为负载均衡把客户端真实IP发送给后端配置

    Nginx作为负载均衡获取到客户端的真实IP,但是后端获取到的IP为nginx负载均衡的IP,需要修改配置使后端获取到客户端的真实IP 修改nginx配置增加3行 proxy_set_header H ...

  4. iOS-25个小技巧

    (一)关于UITableView  方法flashScrollIndicators:这个很有用,闪一下滚动条,暗示是否有可滚动的内容.可以在ViewDidAppear或[table reload]之后 ...

  5. SpringBoot Profile使用详解及配置源码解析

    在实践的过程中我们经常会遇到不同的环境需要不同配置文件的情况,如果每换一个环境重新修改配置文件或重新打包一次会比较麻烦,Spring Boot为此提供了Profile配置来解决此问题. Profile ...

  6. Homebrew介绍和使用

    一.Homebrew是什么 Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径 ...

  7. select 和v-model

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 快速了解NIO

    NIO的由来 我们都知道,在jdk1.4的时候就开始引入NIO了,它是基于Selector机制的非阻塞I/O,可以将多个异步的I/O操作集中到一个或几个线程中进行处理,目的就是为了代替阻塞I/O,提到 ...

  9. SrpingBoot入门到入坟02-HelloWorld的细节和初始自动配置

    关于SpringBoot的第一个HelloWorld的一些细节: 1.父项目 首先查看项目中的pom.xml文件 文件中有个父项目,点进去则: 它里面也有一个父项目,再点进去: 可以发现有很多的依赖版 ...

  10. 【win10】 ffmpeg的安装

    安装肯定要先下载,官方下载地址:http://www.ffmpeg.org/download.html 然后会进入这个页面. 然后根据你的操作系统选择 根据自己系统选择,我的系统是64位的所以下载的是 ...