LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:?https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交換相鄰的兩個節(jié)點 如上圖所示,遞歸進(jìn)行交換。從最尾端開始,當(dāng)最尾端只有一個節(jié)點時,停止交換 否則執(zhí)行 swap(head.next)? 參考代碼: package leetcode_50;/*** * @author pengfei_zheng* 交換相鄰節(jié)點*/
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;}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/zpfbuaa/p/6527363.html
總結(jié)
以上是生活随笔為你收集整理的LeetCode 24 Swap Nodes in Pairs (交换相邻节点)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线通信中FEC 编码原理及评价
- 下一篇: tensorflow的一些函数