题目:swap nodes in pairs

<span style="font-size:18px;">/**
* LeetCode Swap Nodes in Pairs
* 题目:输入一个链表,要求将链表每相邻的两个节点交换位置后输出
* 思路:遍历一遍就可以,时间复杂度O(n)
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
package javaTrain; public class Train12 {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return null;
ListNode pNode;
pNode = head;
while(pNode != null && pNode.next != null){
int temp;
temp = pNode.val;
pNode.val = pNode.next.val;
pNode.next.val = temp;
pNode = pNode.next.next;
}
return head;
}
}
</span>

【LeetCode】Swap Nodes in Pairs 链表指针的应用的更多相关文章

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

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

  2. LeetCode: Swap Nodes in Pairs 解题报告

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

  3. [LeetCode]Swap Nodes in Pairs题解

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

  4. LeetCode Swap Nodes in Pairs 交换结点对(单链表)

    题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...

  5. Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

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

  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] Swap Nodes in Pairs 成对交换节点

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

  8. 24. Swap Nodes in Pairs 链表每2个点翻转一次

    [抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...

  9. leetcode—Swap Nodes in Pairs

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

随机推荐

  1. P2756 网络流解决二分图最大匹配

    P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 P2756 飞行员配对方案问题 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语 ...

  2. sql语句执行顺序与性能优化(1)

    一.首先我们看一下mysql的sql语句的书写顺序 . select--distinct--from--on--where--group by--having--聚合函数cube.rollup--or ...

  3. PXE自动化安装系统

    准备(以centos7为例) ①关闭防火墙 ②关闭selinux ③dhcp服务设置为静态IP ④安装软件包 http:充当yum源安装包仓库 tftp-server :在它的工作目录存在引导主机的工 ...

  4. GO:interface

    一.感受接口 type Usb interface { Connect() Disconnect() } // 手机 type Phone struct {} // 相机 type Camera st ...

  5. 第三章:systemverilog文本值和数据类型

    1.增强的文本值 2.改进的`define文本替换 3.时间值 4.新的变量类型 5.有符号和无符号类型 6.静态和动态变量(***) 7.类型转换 8.常数 增强的文本值(文本赋值增强) 主要是:位 ...

  6. 如何将已经安装从chrome扩展程序导出备份为.CRX文件?

    之前介绍过CRX Extractor可以从chrome应用商店下载备份扩展程序,有读者朋友问说:如果 Google Chrome扩展程序已经从 Chrome应用商店下架,还有没有方法下载呢?通常网路上 ...

  7. Centos 7安装Mysql5.7

    1.下载(国内镜像,比搜狐的快一点):http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc ...

  8. STM32——输入捕获实验原理及配置步骤

    输入捕获实验原理及配置步骤 一.输入捕获概念 STM32的输入捕获,简单的说就是通过检测 TIMx_CHx (定时器X的通道X)上的 边沿信号,在边沿信号发生跳变(比如上升沿/下降沿)的时候,将当前定 ...

  9. Python数据结构--双向链表

    ''' 双向链表包含第一个和最后一个的链接元素. 每个链接都有一个数据字段和两个称为next和prev的链接字段. 每个链接都使用其下一个链接与其下一个链接链接. 每个链接都使用其上一个链接与之前的链 ...

  10. 前端开发利器webStorm 3.0配置使用

     安装了phpstorm之后,想配置svn,结果在file->settings->Version Contorl->subversion->with conmand line ...