[leetcode]24. Swap Nodes in Pairs交换链表的节点
感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时。
最后的思路就是很简单的进行交换,设置一个头结点前边的0节点先把第三个节点接到第一个上边,然后把第一个接到第二个上,然后把第二个节点接到0节点上,然后把当前节点设置成第一个节点(现在是第二个,而且是下次交换的0节点)
public ListNode swapPairs(ListNode head) {
if (head==null) return null;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
while (cur.next!=null&&cur.next.next!=null)
{
ListNode first = cur.next;
ListNode sec = cur.next.next;
first.next = sec.next;
sec.next = first;
cur.next = sec;
cur = first;
}
return dummy.next;
}
[leetcode]24. Swap Nodes in Pairs交换链表的节点的更多相关文章
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 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. 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
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. For example,Given 1->2-&g ...
随机推荐
- 5、pgpool-II高可用性(一)数据库的高可用性
官网示例 一.实现原理 使用 pgpool-II 软件:我们常用来实现流复制的高可用性:备库只读的,不可写:就是当主库出现问题时:需要把备库自动激活为主库:来接管服务. 这在其他高可用软件也有这功能, ...
- Spring Cloud 学习 (二) Ribbon
负载均衡是指将负载分摊到多个执行单元上,常见的负载均衡有两种方式:一种是独立进程单元,通过负载均衡策略,将请求转发到不同的执行单元上,例如 Ngnix:另一种是将负载均衡逻辑以代码的形式封装到服务消费 ...
- socket阻塞与非阻塞,同步与异步,select,pool,epool
概念理解 一.与I/O相关的五个重要概念 1. 第一个概念:用户空间与内核空间 1. 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方) 2. ...
- 单调栈模板 POJ3250
上次二分st表大法失败以后的又一次尝试233333 封装,封装,封装!!!!!! #include <bits/stdc++.h> using namespace std; #define ...
- 第8.33节 Python中__getattr__以及__getattr__与__ getattribute__的关系深入剖析
一. 引言 前面几节分别介绍了Python中属性操作捕获的三剑客:__ getattribute__方法.__setattr__方法.__delattr__方法,为什么__ getattribute_ ...
- SUCTF pythonigx
0x00知识点 nginx配置 配置文件存放目录:/etc/nginx主配置文件:/etc/nginx/conf/nginx.conf管理脚本:/usr/lib64/systemd/system/ng ...
- XSS漏洞防御之HttpOnly
WWW服务依赖于Http协议实现,Http是无状态的协议,所以为了在各个会话之间传递信息,就需要使用Cookie来标记访问者的状态,以便服务器端识别用户信息. Cookie分为内存Cookie和硬盘C ...
- html标签和body标签的区别
首先想要总结这个问题就是因为在开发的过程中,在设置body的高度的时候,在浏览器窗口中并不起作用,一直都会显示是浏览器窗口的大小,所以想要搞清楚这面的原因. 一.前提 在页面的设计中,当我们没有为一个 ...
- 【题解】G.Graph(2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest)
题目链接G题 题意 序列 \(a_1,a_2,⋯,a_n\) 是一个排列, 当且仅当它含有 1 到 n 的所有整数. 排列 \(a_1,a_2,⋯,a_n\) 是一个有向图的拓扑排序,当且仅当对于每条 ...
- kafka命令及启动
默认内网访问,要在外网访问的话,需要在修改config/server.properties中的配置 将listeners和advertised.listeners的值用主机名进行替换,在外用使用jav ...