
1.汉诺塔问题面试题 08.06. 汉诺塔问题 - 力扣LeetCode解法递归class Solution { public void hanota(ListInteger A, ListInteger B, ListInteger C) { bfs(A,B,C,A.size()); } private void bfs(ListInteger a, ListInteger b, ListInteger c, int size) { if (size1){ c.add(a.remove(a.size()-1)); //当只有一个盘子的时候直接将盘子放到c上 return; } bfs(a,c,b,size-1); //将size-1个a上的盘子借助c移动到b上 c.add(a.remove(a.size()-1)); //将a上的盘子直接放到c上 bfs(b,a,c,size-1); //将b上的盘子借助a移动到c上 } }2.合并两个有序链表21. 合并两个有序链表 - 力扣LeetCode解法递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val val; } * ListNode(int val, ListNode next) { this.val val; this.next next; } * } */ class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1null){ return list2; } if (list2null){ return list1; } if (list1.vallist2.val){ list1.nextmergeTwoLists(list1.next,list2); return list1; }else{ list2.nextmergeTwoLists(list1,list2.next); return list2; } } }3.反转链表206. 反转链表 - 力扣LeetCode解法递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val val; } * ListNode(int val, ListNode next) { this.val val; this.next next; } * } */ class Solution { public ListNode reverseList(ListNode head) { if (headnull || head.nextnull){ return head; } ListNode newHeadreverseList(head.next); head.next.nexthead; head.nextnull; return newHead; } }4.两两交换链表中的节点24. 两两交换链表中的节点 - 力扣LeetCode解法递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val val; } * ListNode(int val, ListNode next) { this.val val; this.next next; } * } */ class Solution { public ListNode swapPairs(ListNode head) { if (headnull || head.nextnull){ return head; } ListNode tempswapPairs(head.next.next); //先让后面的节点进行交换 ListNode newHeadhead.next; //标记链表的第二个节点即交换完之后新的头节点 head.nexttemp; //修改指向将头节点的next指向temp newHead.nexthead; //修改指向将newHead的next指向head return newHead; } }5.Pow(x,n)50. Pow(x, n) - 力扣LeetCode解法递归class Solution { public double myPow(double x, int n) { return n0?1/pow(x,n):pow(x,n); } private double pow(double x, int n) { if (n0){ return 1.0; } double temp pow(x,n/2); return n%20? temp*temp : temp*temp*x; } }