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. 如何在Kubernetes里给PostgreSQL创建secret

    创建一个initdb.sql文件,输入如下内容: -- This is a postgres initialization script for the postgres container. -- ...

  2. Android(java)学习笔记20:UDP协议发送数据

    1. UDP协议发送数据:我们总是先运行接收端,再运行发送端发送端: package cn.itcast_02; import java.io.IOException; import java.net ...

  3. swift 协议(结合扩展)的特点

    协议的传统实现: 定义接口+实现协议    由抽象到具体: 协议的逆向实现(使用扩展): 由已存在的类型抽离部分功能作为协议,并让原体符合协议: 由具体到抽象:  向上抽离:  向上生成: 协议的缺省 ...

  4. 【洛谷2709】小B的询问(莫队模板题)

    点此看题面 大致题意: 有一个长度为\(N\)的序列,每个数字在\(1\sim K\)之间,有\(M\)个询问,每个询问给你一个区间,让你求出\(\sum_{i=1}^K c(i)^2\),其中\(c ...

  5. Gym - 101334E 多叉树遍历

    题意:给定一个字符串,求有多少种树与之对应,对应方式是,每次遍历左节点,没有了,就回溯: 分析:d[i,j] = sum(d[i+1,k-1],d[k,j]) (str[i]==str[k]); 坑点 ...

  6. Jmeter 登陆性能测试

    1.打开Jmeter,新建一个线程组:测试计划--添加--Threads(users)---线程组 如图: 2.首先要添加一个HTTP默认请求,为什么要添加这个呢? 如果要测试的系统域名或者IP地址是 ...

  7. 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...

  8. 在CentOs6.5下安装Python2.7.6和Scrapy

    总在网上看大家的安装教程,这回自己也贡献一份!!! 和民航大学合作项目,去给人家装环境,连简单的Scrapy都没装上,虽然有对方硬件设施坑爹的因素,但主要还是自己准备不充分. 一份好的安装文档应该是可 ...

  9. C# 程序启动其他进程程序

    1  启动一个独立进程,需要用到的命名空间是:using System.Diagnostics;   进程类是 Process ,进程的相关参数信息类是 ProcessStartInfo 2  等待启 ...

  10. static函数

    C语言中使用静态函数的好处:      静态函数会被自动分配在一个一直使用的存储区,直到退出应用程序实例,避免了调用函数时压栈出栈,速度快很多.       关键字“static”,译成中文就是“静态 ...