1. 原题链接

https://leetcode.com/problems/swap-nodes-in-pairs/description/

2. 题目要求

给定一个链表,交换相邻的两个结点。已经交换的结点,不再进行交换。

注意:所使用的空间大小固定

例如,1->2->3->4转换后为2->1->4->3

3. 题目思路

使用一个遍历指针current和两个辅助指针first、second,first保存current指针所在结点的后继结点,second保存current指针所在结点的后继的后继结点。

令first的后继指向second的后继,current的后继等于second,current后继的后继等于first,current等于first

4. 代码实现

public class SwapNodeInPairs24 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
ListNode l6 = new ListNode(6);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6; ListNode ls1 = l1;
while (ls1 != null) {
System.out.print(ls1.val);
ls1 = ls1.next;
}
System.out.println(""); ListNode ls2 = swapPairs(l1);
while (ls2 != null) {
System.out.print(ls2.val);
ls2 = ls2.next;
} } public static ListNode swapPairs(ListNode head) {
ListNode headPointer = new ListNode(0);
headPointer.next = head;
ListNode current = headPointer; while (current.next != null && current.next.next != null) {
ListNode first = current.next;
ListNode second = current.next.next;
first.next = second.next;
current.next = second;
current.next.next = first;
current = first;
} return headPointer.next;
} public static class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
}
}

  

LeetCode:24. Swap Nodes in Pairs(Medium)的更多相关文章

  1. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  2. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  3. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

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

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

  5. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

  6. 【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 ...

  7. LeetCode OJ 24. Swap Nodes in Pairs

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

  8. 【leetcode】Swap Nodes in Pairs (middle)

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

  9. leetcode 之Swap Nodes in Pairs(21)

    不允许通过值来交换,在更新指针时需要小心. ListNode *swapNodes(ListNode* head) { ListNode dummy(-); dummy.next = head; fo ...

随机推荐

  1. 每天一个linux命令:du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  2. POJ 1379 模拟退火

    模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举, ...

  3. 百度提供的LBS服务

    并不是所有 LBS 云服务 都可以使用 js Ajax 访问,涉及跨域问题 (Jsonp 方式解决)Jsonp 解决跨域问题原理,在页面生成<script> 加载远程 js 代码片段.在L ...

  4. Android学习笔记_60_Android常用ADB命令

    第一部分: 1. ubuntu下配置环境anroid变量: 在终端执行 sudo gedit /etc/profile 打开文本编辑器,在最后追加#set android environment 2. ...

  5. 协议类接口 - UART

    一.何为协议类接口? 双方约定信号的协议和满足时序要求. 二.UART如何传数据 通用异步收发器简称 UART,即“Universal Asynchronous Receiver Transmitte ...

  6. Flask—05-理解掌握flask数据模型(01)

    数据模型 数据库回顾 分类: 关系型数据库:MySQL.sqlite.… 非关系型数据库:Redis.MongoDB.… 操作: 执行原生SQL语句,每次都需要拼接SQL语句,非常繁琐而且特别容易出错 ...

  7. sql*plus

    [sql*plus创建txt文档编辑sql语句] (1)创建一个txt,命名doc SQL> ed  doc;          /*ed   文件名*/ (2)在doc.txt文件编辑sql语 ...

  8. c# 分析SQL语句中的表操作

    最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...

  9. #leetcode刷题之路2-两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  10. 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

    Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...