递归的一些练习题(自学版附带灵魂画师图解)

这篇博客主要介绍了几个关于链表的算法练习题目,包括汉诺塔问题、合并两个有序链表、移除链表元素、反转链表等。每个题目都提供了详细的题目描述,并探讨了解题思路。
摘要由CSDN通过智能技术生成

先浅尝一个汉诺塔(已刷2)

public class Solution{

  public static void main(String args[]){
    hanoi(2, 'A', 'B', 'C');

  }

  public static void hanoi(int n, char a, char b, char c) {
    if (n == 1) {
      System.out.println("第1块从"+ a + "到"+ c);
    }else {
      hanoi(n-1, a, c, b);
      System.out.println("第"+n+"块从"+ a + "到"+ c);
      hanoi(n-1, b, a, c);
    }
  }
}

合并两个有序链表(刷2)

题目

/**
 * 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 (list1 == null) {
            return list2;
        }else if(list2 == null) {
            return list1;
        }else if(list1.val <= list2.val){
        // 这里其实就是一个概括性的步骤,我们并不去仔细想调用自身又干了什么
        // 概括性的步骤意思就是: 如果我当前l1的值小于l2的值,那说明当前
        //l1一定是在l1剩下的和整个l2完成合并之前

            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        }else{
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }

    }
}

移除链表元素(刷2)

题目

/**
 * 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 removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        else if (head.val == val && head.next == null) {
            return null;
        }else if(head.val == val && head.next != null) {
            return removeElements(head.next, val);
        }
        else{
            head.next = removeElements(head.next, val);
            return head;
        }

    }
}

反转链表(刷2)

题目

/**
 * 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(head == null || head.next == null) {
            return head;
        }
        else {
            ListNode cur = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return cur;
        }

    }
}

2 的幂

题目

class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n == 0){
            return false;
        }else if (n == 1){
            return true;
        }else if (n % 2 == 0 && isPowerOfTwo(n/2)) {   
                return true;
        }else {
            return false;
        }

    }
}

3 的幂

题目

class Solution {
    public boolean isPowerOfThree(int n) {
        if(n == 0 || n == 2) {
            return false;
        }else if (n == 1) {
            return true;
        } else if (n % 3 == 0 && isPowerOfThree(n/3)) {   
                return true;
        }else {
            return false;
        }

    }
}

4 的幂

题目

class Solution {
    public boolean isPowerOfFour(int n) {
        // 方法一,除到底
    //     while(n != 0 && n % 4 == 0) {
    //         n = n / 4;
    //     }
    //     return n == 1;

    // 方法二, 递归
    if (n < 4 && n != 1) {
        // 0 1 2 3
        return false;
    }else if (n == 1) {
        return true;
    }else if(n % 4 == 0 && isPowerOfFour(n/4)){
        return true;
    }else{
        return false;
    }
    }
    
}

反转字符串

题目

class Solution {
    char t = 0;
    public void reverseString(char[] s) {
          reverse(s, 0, s.length - 1);
        }

    public void reverse(char[] s,int begin,int end) {
        if (end - begin >= 1) {
            // 交换首位
            t = s[end];
            s[end] = s[begin];
            s[begin] = t;
            reverse(s, begin+1, end - 1);
        }else {
            return;
        }
        }
    
}

进阶版

1. 两数相加

题目

/**
 * 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 addTwoNumbers(ListNode l1, ListNode l2) {
        return r(l1,l2,0);
    }

    private ListNode r(ListNode l1,ListNode l2,int add){
        if(l1 == null && l2 == null && add == 0) {
            return null;
        }
        int val = (l1 == null? 0: l1.val) + (l2==null ? 0: l2.val) + add;
        add = val >= 10 ? 1 : 0;
        ListNode x = new ListNode(val % 10);
        x.next = r(l1==null?null:l1.next,l2==null?null:l2.next, add);
        return x;
        
    }

}

2. 重排链表

题目

/**
 * 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 void reorderList(ListNode head) {
        if(head.next == null || head.next.next == null) {
            return;
        }else{
            ListNode end = getEnd(head);
            ListNode end2 = get2End(head);
            end.next = head.next;
            head.next = end;
            end2.next = null;
            reorderList(head.next.next);
        }
    }
    
    public ListNode getEnd(ListNode head) {
        // 给定一个链表得到他的尾指针
        while(head.next != null){
            head = head.next;
        }
        return head;
    }
    public ListNode get2End(ListNode head) {
        // 给定一个链表得到他的倒数第二指针
        while(head.next.next != null){
            head = head.next;
        }
        return head;
    }
}

一种非常好的思路

/**
 * 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 void reorderList(ListNode head) {
        if(head.next == null || head.next.next == null) {
            return;
        }else{
            List<ListNode> l = new ArrayList<ListNode>();
            ListNode cur = head;
            while(cur != null) {
                l.add(cur);
                cur = cur.next;
            }
            int i = 0;
            int j = l.size() - 1;
            while(i < j) {
                l.get(i).next = l.get(j);
                i++;
                if(i == j) {
                    break;
                }else{
                    l.get(j).next = l.get(i);
                    j--;
                }

            }
            l.get(i).next = null;
        
            
        }
    }
    
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值