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的更多相关文章

  1. 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 ...

  2. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  3. [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 ...

  4. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

  5. 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 ...

  6. (链表 递归) 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 ...

  7. [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 ...

  8. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  9. [LeetCode] 24. Swap Nodes in Pairs ☆

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

随机推荐

  1. C语言 字符串前加L的意义 如:L“A”

    转自:http://c.biancheng.net/cpp/html/1069.html Unicode或者宽字符都没有改变char数据型态在C中的含义.char继续表示1个字节的储存空间,sizeo ...

  2. webpack学习(二):先写几个webpack基础demo

    一.先写一个简单demo1 1-1安装好webpack后创建这样一个目录: 1-2:向src各文件和dist/index.html文件写入内容: <!DOCTYPE html> <h ...

  3. css边框小结

    css边框 CSS对界面的分割如上图,他们的含义如下: contend:包含HTML元素中包含的文本,图像或其他媒体.      padding:内容和边框之间的空格. 你可以想像这样的内在空间.   ...

  4. 论文笔记(1):From Image-level to Pixel-level Labeling with Convolutional Networks

    文章采用了多实例学习(MIL)机制构建图像标签同像素语义的关联 . 该方法的训练样本包含了70 万张来自ImageNet的图片,但其语义分割的性能很大程度上依赖于复杂的后处理过程,主要包括图像级语义的 ...

  5. 关于使用Ajax请求json数据,@RequestMapping返回中文乱码的几种解决办法

    一.问题描述: 使用ajax请求json数据的时候,无论如何返回的响应编码都是ISO-8859-1类型,因为统一都是utf-8编码,导致出现返回结果中文乱码情况. $.ajax({ type:&quo ...

  6. 伯克利推出世界最快的KVS数据库Anna:秒杀Redis和Cassandra

    天下武功,唯快不破. 伯克利 RISE 实验室推出了最新的键值存储数据库 Anna,提供了惊人的存取速度.超强的伸缩性和史无前例的一致性保证.Jeff Dean 说,当一个系统增长到十倍规模时,就需要 ...

  7. NancyFX 第三章 Web框架

    如果使用Nancy作为一个WEB框架而言,会有什么不同?实际上很多. 在使用Nancy框架为网页添加Rest节点和路由和之前的Rest框架中是相同的,这方面没有什么需要学习的了.Nancy采用一贯的处 ...

  8. LCA倍增算法

    LCA 算法是一个技巧性很强的算法. 十分感谢月老提供的模板. 这里我实现LCA是通过倍增,其实就是二进制优化. 任何一个数都可以有2的阶数实现 例如16可以由1 2 4 8组合得到 5可以由1 2 ...

  9. 数据分析之Pandas和Numpy学习笔记(持续更新)<1>

    pandas and numpy notebook        最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...

  10. JavaScript 数组基础知识

    // Array 类型: // 检测数组: // console.log(myarr instanceof Array) //true // toString()方法会返回由数组中每个值的字符串形式拼 ...