LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
2017-03-09 19:35
561 查看
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description
Problem: 交换相邻的两个节点
如上图所示,递归进行交换。从最尾端开始,当最尾端只有一个节点时,停止交换
否则执行 swap(head.next)
参考代码:
Problem: 交换相邻的两个节点
如上图所示,递归进行交换。从最尾端开始,当最尾端只有一个节点时,停止交换
否则执行 swap(head.next)
参考代码:
package leetcode_50; /** * * @author pengfei_zheng * 交换相邻节点 */ public class Solution24 { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode swapPairs(ListNode head) { if ((head == null)||(head.next == null)) return head; ListNode n = head.next; head.next = swapPairs(head.next.next); n.next = head; return n; } }
相关文章推荐
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
- [C++]LeetCode: 109 Swap Nodes in Pairs (交换相邻节点位置)
- LeetCode-24 Swap Nodes in Pairs(链表中按要求交换节点)
- [leetcode] 24 Swap Nodes in Pairs(交换链表相邻结点)
- LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)(Linked List)
- (Java) LeetCode 24. Swap Nodes in Pairs —— 两两交换链表中的节点
- Swap Nodes in Pairs 链表交换相邻的节点
- LeetCode 24. Swap Nodes in Pairs(交换节点)
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
- LeetCode 24. Swap Nodes in Pairs ***** 双节点交换
- [LeetCode] Swap Nodes in Pairs(成对交换节点)
- [leetcode 24]Swap Nodes in Pairs-----成对翻转链表中的节点
- LeetCode - 24 - Swap Nodes in Pairs(交换链表结点)
- LeetCode Swap Nodes in Pairs交换链表中的两个节点
- [LeetCode] 24. Swap Nodes in Pairs 成对交换节点
- [LeetCode] Swap Nodes in Pairs 成对交换节点
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
- Swap Nodes in Pairs 链表两两交换节点@LeetCode
- Leet Code 24 Swap Nodes in Pairs - 成对交换节点 - Java
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置