题目: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>

最新文章

  1. Zip加密
  2. Android系统学习小记
  3. Viewbox在UWP开发中的应用
  4. (转载)javascript函数作用域和提前声明
  5. 使用Git push时出现的一些问题处理
  6. Spring Data Elasticsearch
  7. nodejs安装过程及视频地址
  8. 操作jQuery集合搜索父元素
  9. angularjs三级联动
  10. Session的两种实现
  11. JS的DOM操作及动画
  12. PHP求解一个值是否为质数
  13. 我的 FPGA 学习历程(05)—— 使用 Modelsim 仿真工具
  14. Java中判断对象是否为空的方法
  15. Linux - 其他命令
  16. Nancy 寄宿IIS
  17. “数学口袋精灵”第二个Sprint计划---第一天
  18. LeetCode 39 Combination Sum(满足求和等于target的所有组合)
  19. 算法提炼是落脚点-php数组-字符串函数
  20. C/C++ 宏

热门文章

  1. tensorflow note
  2. Python---哈夫曼树---Huffman Tree
  3. pytorch系列 -- 9 pytorch nn.init 中实现的初始化函数 uniform, normal, const, Xavier, He initialization
  4. windows cmd color颜色设置
  5. 纯html+css中实现静态选座位效果技巧(input+label使用小技巧)
  6. mysql查询表中最小可用id值
  7. 免费开源《OdooERP系统部署架构指南》试读:第一章 Odoo架构概述
  8. LeetCode(18)4Sum
  9. cherrypy入门
  10. 大数据学习——mapreduce案例join算法