Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
分析:调换每各pair中的两个node的位置,并返回新的首节点,不能直接改变node的值,而是要调整node的位置。这里要注意描述,调整pair中两个node的位置,返回新的首节点,一开始我只是调整完并没有返回新的首节点,结果几次报错都不知道为什么,明明调试出来的结果是正确的,看清题目很重要。其实这个题目的思路很简单,找到要调换的位置,然后调换一下就可以了,注意临界条件。先上我的代码。
public ListNode swapPairs(ListNode head) {
if (head == null|| head.next == null)
return head;
ListNode finalHead=head.next;
int index = 1;
ListNode headBefore = null;
ListNode headBeforeBefore = null;
while (head!=null&&(head.next != null||(head.next==null&&headBefore.next!=null))) {
if (index % 2 == 0) {
//调换pair中的位置
if(headBeforeBefore!=null)
headBeforeBefore.next=head;
headBefore.next=head.next;
head.next=headBefore;
//调整新的head headB headBB的指代
headBeforeBefore=head;
head=headBefore.next;
index++;
}else {
//调整新的head headB headBB的指代
headBeforeBefore = headBefore;
headBefore = head;
head=head.next;
index++;
}
}
return finalHead;
}
声明了两个节点,一个headB,一个headBB,代表head的父节点与head的祖父节点,然后index计数,每到2进行调换。其实后来想了一下,可以不用这样,在进行调换完成之后,直接定位到下一次要调换的位置,然后操作即可,这样会更快一点。每次调换之后,设置新的head、headB、headBB位置。
A掉之后看了别人的代码,简洁明了,用了递归。
public static ListNode swapPairs(ListNode head) {
if(head==null||head.next==null) {
return head;
}
ListNode n=head.next;
head.next=swapPairs(head.next.next);
n.next=head;
return n;
}
思路是设置完自身后,调用下一个要调换位置节点的方法。
Leetcode 24——Swap Nodes in Pairs的更多相关文章
- 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 ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- [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 ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
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
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交换节点对
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-& ...
随机推荐
- ASP.NET Core轻松入门之Configure中IHostingEnvironment和IApplicationLifetime的使用
在StratUp.cs的Configure方法中,除了 常用的参数除了IApplicationBuilder和在我上一篇文章中提到的Iconfiguration点击打开链接 外 还有 IHostin ...
- ajax页面跳转(后台返回的是一个url地址,或者自己传进去的是url地址)
function modifyMerchantInfo(merchant_code) { $.ajax({ url: '/intra/crm/merchant/OrderMgr.htm?method= ...
- json格式数据整理
一,json的两种数据结构 1.1,对象 对象结构以"{"大括号开始,以"}"大括号结束.中间部分由0或多个以","分隔的"key ...
- TCP/IP资料整理
一.概念 1. TCP/IP常见帧格式 2. 详解网络嗅探工具的原理 二.API 1. Linux kernel tcp 连接建立详解——由listen系统调用的backlog参数引发的长篇大论
- div里面的图片垂直居中
外面一个div, 里面是一张大小不固定的图片, 或者span,div都行, 要实现里面元素水平垂直居中对齐 其实有很多实现的方法, 比如用js去计算每一个子元素的高度, 通过js去动态设置magin值 ...
- ubuntu 更改默认亮度
chmod 777 /sys/class/backlight/intel_backlight/brightnes chmod 777 /sys/class/backlight/intel_backli ...
- [Luogu3455][POI2007]ZAP-Queries
BZOJ(权限题) Luogu 题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Securit ...
- UWP 使用UCT的Markdown控件
之前在网上偶然碰到过 一个在线的Markdown Text编辑器 http://mahua.jser.me/,功能很齐全. 然后就突然有了一个大胆的想法 这个玩意要是在uwp中实现,用来做更新日志说明 ...
- ccd采集
- 为hadoop集群设置静态IP
-更新2017年3月1日19:38:49-- 以下是Nat 网络设置,如果你想要同一局域网内的主机可以远程连接上你的集群环境,建议使用桥接模式,具体原因可看文末三种网络连接模式的区别. 由于之前设置集 ...