[LeetCode] 24. 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 ☆☆☆(链表,相邻两节点交换)的更多相关文章
- 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 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 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 ...
- LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- (链表 递归) 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 ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
- LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...
- [leetcode]24. Swap Nodes in Pairs交换链表的节点
感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...
随机推荐
- 用Dev C++编写第一个C语言程序
不少新同学表示对计算机类专业有些担忧,那么可以趁军训期间提前玩玩. 学校一般使用VS2008进行编程,VS2008功能强大,内容丰富,体积也比较大.如果只关注C语言编程的话,有一个非常轻便的软件Dev ...
- 数据结构与算法 Javascript描述
数据结构与算法系列主要记录<数据结构与算法 Javascript描述>学习心得
- iOS-OC的MRC和ARC内存管理机制
1. Objective-c语言中的MRC(MannulReference Counting) 在MRC的内存管理模式下,对变量的管理相关的方法有:retain,release和autorelease ...
- 【c# 学习笔记】显示接口实现方法
在接口 一张中,使用了隐式的接口实现方式,即在实现代码中没有制定实现那个接口中的CompareTo方法.相应地,自然就有显式的 接口实现方式,它指的是在实现过程中,明确指出实现哪一个接口中的哪一个方 ...
- 入行IT的选择决定了日后走的路的长度和领域的深度
前段时间和发小聊天时,他说了一句话我觉得很值得思考,送给大家:机遇大于努力,选择大于机遇. 一年前我毅然辞去了之前的工作,只身来到北京,正式成为了北漂的一员.对于我们现在的大环境下,其实北漂已经和以前 ...
- thinkphp5 验证器 validate 和 layer
首先tp5的验证器使用特方便 设置规则即可通用 首先页面html(layer 配合) 毕竟是后端 尽量用一些成熟的前台框架 之前用boostrap $.ajax({ url:'/index/Regi ...
- Redis内存数据库的基本语法
Redis: - nosql数据库,非关系型数据库 - 支持5大数据类型 (字符串String,列表list.字典hash,集合set,zset) - 与之相似的有memcache,但memcache ...
- HTML让字体闪动和滚动显示
存粹的HTML让字体闪动显示: <html> <head> <title>TEST</title> <style type="text/ ...
- Flink SQL项目实录
一.Flink SQL层级 为Flink最高层的API,易于使用,所以应用更加广泛,eg. ETL.统计分析.实时报表.实时风控等. Flink SQL所处的层级: 二.Flink聚合: 1.Wind ...
- Asp.Net Core文件上传
文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 I ...