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. 【转载】#443 - An Interface Cannot Contain Fields

    An interface can contain methods, properties, events or indexers. It cannot contain fields. interfac ...

  2. windows下makefile命令详解

    转自https://blog.csdn.net/xiexievv/article/details/45775005 1. 如果已经有vc6的dsp工程,可直接导出nmake脚本文件(.mak) “Pr ...

  3. UVA1184 Air Raid

    嘟嘟嘟 最小路径覆盖板子题. 建二分图,然后跑Dinic(因为我不会匈牙利),然后ans = n - maxflow(). 主要是发一下用链前存图的写法.(好像比vector短一点) #include ...

  4. VMware虚拟机修改BIOS启动项

    vmware默认是硬盘启动,要进bios里面设置成开机的启动顺序,要将光盘设置成第一启动项.但vm的开机画面比笔记本的还要快很多,基本都在1s内的,想进入 bios里面也有难度.. 对于网上说的开vm ...

  5. JavaFXML实现新窗口打开

    实现原理顺着往下看就明白了,流程看红色字体.具体还有什么问题可以留言. 主页面配置文件,一共三个按钮.这里说明第一个按钮触发打开新窗口 <?xml version="1.0" ...

  6. spring入门(六) spring mvc+mybatis

    1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...

  7. udt的java版本judt项目持续升级1.2版本

    修改了一些问题,努力兼容udt4版本.具体内容查看项目更新说明: 当前项目版本1.2 地址:https://github.com/jinyuttt/judt

  8. HDU1159(LCS)

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  9. ABAP术语-Business Object Builder

    Business Object Builder 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/09/1031357.html Tool fo ...

  10. 使用zxing二维码识别

    1.多二维码识别 (同一张图片中多二维码识别) 直接上代码舒服: pom文件: <!-- QR Code --> <dependency> <groupId>com ...