Leetcode 24.Swap Nodes in Pairs
来源:互联网 发布:什么是js模块化 编辑:程序博客网 时间:2024/05/21 10:06
题目链接 Leetcode 24. Swap Nodes in Pairs
给你一个链表,交换相邻两个节点,例如给你 1->2->3->4,输出2->1->4->3。
我代码里在head之前新增了一个节点newhead,其实是为了少写一些判断head的代码。
public class Solution { public ListNode swapPairs(ListNode head) { ListNode p = head; ListNode pre = new ListNode(0); pre.next = head; ListNode newhead = pre; while (null != p && null != p.next) { //两两节点做交换 //其实交换两个节点涉及三个节点的变更 ListNode q = p.next; pre.next = p.next; p.next = q.next; q.next = p; pre = p; p = p.next; } return newhead.next; }}
阅读全文
0 0
- [LeetCode]24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs Leetcode Python
- LeetCode --- 24. Swap Nodes in Pairs
- LeetCode 24.Swap Nodes in Pairs
- [Leetcode] 24. Swap Nodes in Pairs
- 【LeetCode】24.Swap Nodes in Pairs
- [leetcode] 24.Swap Nodes in Pairs
- **LeetCode 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs LeetCode
- leetcode 24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- LeetCode--24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- 【LeetCode】24. Swap Nodes in Pairs
- [leetcode] 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- LeetCode-24.Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- LeetCode:maxi&mum depth of binary tree&balanced-binary-tree
- String\StringBuffer\StringBuilder的不同 基本常用类 Date时间类 Math数学类的基本方法
- 在eclipse中设置了编码方式为utf-8,但是在创建properties文件时没有进行变化
- ZOJ 3960What Kind of Friends Are You?
- eclipse 断点调试方法
- Leetcode 24.Swap Nodes in Pairs
- 最大字段和算法
- Bigdata Development Java_Study_06(集合Collection和Map)
- Immutable.js使用
- 多线程的那点儿事(之读写锁)
- 【QT】Qt creator自定义编译运行
- java中的匿名内部类总结
- C/C++笔试题
- python学习-面向对象进阶之元类(三)