[LeetCode]24. Swap Nodes in Pairs
来源:互联网 发布:数据安全 国家安全法 编辑:程序博客网 时间:2024/05/29 02:03
https://leetcode.com/problems/swap-nodes-in-pairs/
链表可拆解问题依然两解,链表问题常要新加一个头结点
递归
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { return head; } ListNode next = swapPairs(head.next.next); ListNode res = head.next; res.next = head; head.next = next; return res; }}
非递归
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode swapPairs(ListNode head) { ListNode pre = new ListNode(0); pre.next = head; ListNode cur = pre; while (cur.next != null && cur.next.next != null) { ListNode first = cur.next; ListNode second = cur.next.next; first.next = second.next; second.next = first; cur.next = second; cur = cur.next.next; } return pre.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
- Node.js的读取文件的异步过程基础的讲解
- COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4'
- 查找
- leetcode--Move Zeroes
- Qt 贪吃蛇小游戏
- [LeetCode]24. Swap Nodes in Pairs
- java入门实例 奥特曼打小怪兽
- [dfs树 神奇思路] Codeforces 730K NEERC16 Southern Subregional K. Roads Orientation Problem
- 函数的返回值和临时对象的问题
- [OpenGL]从零开始写一个Android平台下的全景视频播放器——5.1 使用OpenGL把全景视频贴到球上
- 汇编语言实现小写转为大写
- URL请求后台获取不到特殊字符的参数
- 自己写的缓存
- svm——(二)