LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:?https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description 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;}
}
?
轉載于:https://www.cnblogs.com/zpfbuaa/p/6527363.html
總結
以上是生活随笔為你收集整理的LeetCode 24 Swap Nodes in Pairs (交换相邻节点)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线通信中FEC 编码原理及评价
- 下一篇: PHP 微信机器人 Vbot 结合 La