【LeetCode】Swap Nodes in Pairs 链表指针的应用
2024-08-30 18:16:36
<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>
最新文章
- Zip加密
- Android系统学习小记
- Viewbox在UWP开发中的应用
- (转载)javascript函数作用域和提前声明
- 使用Git push时出现的一些问题处理
- Spring Data Elasticsearch
- nodejs安装过程及视频地址
- 操作jQuery集合搜索父元素
- angularjs三级联动
- Session的两种实现
- JS的DOM操作及动画
- PHP求解一个值是否为质数
- 我的 FPGA 学习历程(05)—— 使用 Modelsim 仿真工具
- Java中判断对象是否为空的方法
- Linux - 其他命令
- Nancy 寄宿IIS
- “数学口袋精灵”第二个Sprint计划---第一天
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
- 算法提炼是落脚点-php数组-字符串函数
- C/C++ 宏
热门文章
- tensorflow note
- Python---哈夫曼树---Huffman Tree
- pytorch系列 -- 9 pytorch nn.init 中实现的初始化函数 uniform, normal, const, Xavier, He initialization
- windows cmd color颜色设置
- 纯html+css中实现静态选座位效果技巧(input+label使用小技巧)
- mysql查询表中最小可用id值
- 免费开源《OdooERP系统部署架构指南》试读:第一章 Odoo架构概述
- LeetCode(18)4Sum
- cherrypy入门
- 大数据学习——mapreduce案例join算法