为什么80%的码农都做不了架构师?>>>   

问题:

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.

解决:

① 直接交换即可。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution { //4ms
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast != null && fast.next != null && fast.next.next != null){//避免空指针异常
            int tmp = slow.val;
            slow.val = fast.val;
            fast.val = tmp;
            slow = fast.next;
            fast = slow.next;
        }
        int tmp = slow.val;//处理最后两个数
        slow.val = fast.val;
        fast.val = tmp;
        return head;
    }
}

class Solution {//5ms
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode header = new ListNode(-1);
        header.next = head;
        ListNode cur = header;
        ListNode slow = head;
        ListNode fast = head.next;
        while(slow != null && fast != null){
            slow.next = fast.next;
            fast.next = slow;
            cur.next = fast;
            cur = slow;
            slow = slow.next;
            if(slow != null)fast = slow.next;
        }
        return header.next;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1527880

成对的交换链表的节点 Swap Nodes in Pairs相关推荐

  1. C语言链表交换相邻节点,LeetCode 24--两两交换链表中的节点 ( Swap Nodes in Pairs ) ( C语言版 )...

    题目描述  : 解题思路 : 分为两种情况处理 , 当要交换的节点是链表的前两个节点时 , 当交换的节点非前两个节点时 ; 代码如下 : /** * Definition for singly-lin ...

  2. 【重点】LeetCode 24. Swap Nodes in Pairs

    LeetCode 24. Swap Nodes in Pairs 参考网址:http://www.cnblogs.com/grandyang/p/4441680.html 此题算是链表中比较考察细节的 ...

  3. 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. 5.18 优先队列(堆) 滑动窗口(二) 交换链表的节点

    295. 数据流的中位数 最简单的思路暴力法,每次读入数据都进行排序 但是中位数只对中间的一个或两个数据感兴趣,其他数没有必要进行交换或者比较 https://leetcode-cn.com/prob ...

  5. Leetcode 24.两两交换链表的节点 (每日一题 20210624)

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表.你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换.示例 1:输入:head = [1,2,3,4] 输出:[2,1,4,3] 示例 ...

  6. leetcode24题:两两交换链表的节点

    题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 输入:head = [1,2,3,4] 输出:[2,1, ...

  7. Leetcode 24. Swap Nodes in Pairs

    不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 本题很简单,大致翻译过来的意思是将单链表相邻节点互换节点位置.额外的要求是使用O(1)空间,以及不修改节点值,仅对节 ...

  8. Leetcode24.Swap Nodes in Pairs两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  9. [LeetCode]两两交换链表中的节点(Swap Nodes in Pairs)

    题目描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能 ...

最新文章

  1. 常用的数据结构-数组
  2. 请填写红包接口调用ip_SOME/IP的车载网络应用
  3. java web方面杂志_环境方面比较好发的期刊_全球环境类最好的期刊_环境领域top期刊...
  4. SQL Server 2005中创建CLR存储过程
  5. inline函数包涵static变量,调用时是否会有多份拷贝
  6. 如何修改vs2008代码字体
  7. 【JS 逆向百例】转变思路,少走弯路,X米加密分析
  8. UI版式设计模板,这样做才高级!
  9. 3 photolemur 样式_macOS下支持RAW格式的照片编辑工具
  10. 中国近代史-蒋廷黻-笔记-第一章-剿夷与抚夷-第二节-英国人做鸦片买卖
  11. 8cm等于多少像素_PPT尺寸你们都设置成多少(我问的不是分辨率像素,而是长、高尺寸)?...
  12. ENVI_建模工具的使用——以“指数计算”批处理为例
  13. 湖北农商行计算机类笔试题,2019年湖北农商行笔试入门汇总提前知~
  14. 有效扩展:来自预训练和微调变换器的见解、rct.ai训练出5亿参数的BERT-X模型
  15. 【操作系统】操作系统极速入门
  16. 计算机毕业设计springcloud房产销售平台
  17. NLP中的数据增强方法
  18. 一文看懂麒麟9000:153亿晶体管,刷新5G速度,还有更强游戏体验,并且会“继续前行”...
  19. 计算机操作题蝴蝶效应,办公自动化上机操作测试题
  20. 雷神之锤冠军游戏角色高清Mac动态壁纸

热门文章

  1. 如何自学python数据分析-『』python数据分析该怎么入门呢?
  2. 小学生学python-小学生就学编程,就学Python,真的那么重要吗?
  3. 路由在express中的实践
  4. maven创建多模块项目
  5. VS2010编译出现APPCRASH问题
  6. 行为模式之Visitor模式
  7. 网络编程学习笔记(ICMPv6和IPv6套接口选项)
  8. 4.11 日期/时间的程序
  9. wordpress-基础插件,常用函数
  10. poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】