【LeetCode】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 链表指针的应用的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 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 ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 24. Swap Nodes in Pairs 链表每2个点翻转一次
[抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
随机推荐
- QT+ 使用标准对话框+关于对话框+问题对话框+文件对话框
#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAct ...
- Oracle清空数据库中数据表数据的方法
一.简介最近在项目发版测试的时候,导出dmp的时候不小心把开发库中的一些脏数据导出来了,测试那边导入进去之后一堆不规范的数据,为了不影响测试结果,于是总结了一个快速清空数据库数据表所有数据的方法. 二 ...
- PHP100视频教程-->视频下载
链接:https://pan.baidu.com/s/14tbX1rz3hYSKY6k0T6WVzg提取码:kypy PHP是一种目前最流行的服务端Web程序开发语言之一.PHP主要的特点是语法简单易 ...
- Yii1 用commandBuilder方法往数据表中插入多条记录
$builder = Yii::app()->db->schema->commandBuilder; // 创建builder对象 $command = $builder->c ...
- [LOJ] 分块九题 6
单点插入,单点查询. 优化了的链表. 链表老写错,干脆用vector,也不算慢. 注意链表退化的问题,及时(比如操作根号n次)就重新建块,实测速度可以提高一倍,这还是数据随机的情况,若涉及大量同一位置 ...
- mysql恢复数据
1.崩溃恢复: 突然断电.宕机,导致mysql无法正常启动: (1) 关闭数据库. (2) Vim /etc/my.cnf 添加:innodb_force_recovery=1 默认为0. 1( ...
- 条款20:宁以pass-by-reference-to-const替换pass-by-value(Prefer pass-by-reference-to-const to pass-by-value)
NOTE: 1.尽量以pass-by-reference-to-const 替换pass-by-value.前者通常比较高效,并可避免切割问题(slicing problem). 2.以上规则并不适用 ...
- c#导出word文档
为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法 public class BiultReportForm { /// <summary>word 应用对象 & ...
- Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)
1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...
- hadoop格式化出错,提示IO异常
配置好hadoop之后,在进行格式化的时候出现异常,原因是由于在core-site.xml 配置文件中写的路径格式不对. 不需要加 file:/ 或者 file:// 直接写绝对路径就行. <c ...