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-& ...
 
随机推荐
- HTML5图片居中的问题
			
刚开始接触html5,准备写一些网页,但是学习的过程中遇到了图片不能居中的问题,首先来看看,代码和执行效果: <!DOCTYPE html> <html> <head&g ...
 - Struts+Spring+Hibernate、MVC、HTML、JSP
			
javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...
 - javascript中的内存管理和垃圾回收
			
前面的话 不管什么程序语言,内存生命周期基本是一致的:首先,分配需要的内存:然后,使用分配到的内存:最后,释放其内存.而对于第三个步骤,何时释放内存及释放哪些变量的内存,则需要使用垃圾回收机制.本文将 ...
 - Dynamics 365中审核用户权限变化的一种方法
			
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复268或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...
 - SpringBoot集成Shiro并用MongoDB做Session存储
			
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
 - 【NOIP2012】开车旅行(倍增)
			
题面 Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i的海拔高度为Hi,城市 ...
 - Bzoj4199:[NOI2015]品酒大会
			
题面 Bzoj4199 Sol 后缀数组 显然的暴力就是求\(LCP\)+差分 \(40\)分 # include <bits/stdc++.h> # define RG register ...
 - [BZOJ1503] [NOI2004] 郁闷的出纳员 (treap)
			
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
 - [BZOJ2743] [HEOI2012] 采花 (树状数组)
			
Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一 ...
 - Xampp相关命令
			
http://www.upwqy.com/details/5.html 1 启动关闭 需要切换目录: # cd /opt/lampp/ 启动 XAMPP # ./lampp start 停止 XAMP ...