链表篇(5)—— leetcode 链表第203题 递归解法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86708695

1 递归求解 leetcode第203

1.1 递归测试

  • 递归的要素
    在这里插入图片描述
package linked_list_leetcode;
/*
 *
 * 用递归的方式算 arr 数组的和
 * */
public class Sum {

    public static int sum(int[] arr) {
        return sum(arr, 0);
    }

    // 计算 arr[l...n] 这个区间内所有数字的和
    private static int sum(int[] arr, int l) {
        if (l == arr.length) {
            return 0;
        }

        return arr[l] + sum(arr, l + 1);
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(sum(nums));
    }
}

1.2 链表去除元素递归法

在这里插入图片描述

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    public ListNode(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("arr can not be empty!");
        }

        this.val = arr[0];

        ListNode cur = this;

        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();

        ListNode cur = this;
        while (cur != null) {
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("NULL");

        return res.toString();
    }
}

public class RemoveElements_203 {
    /*
     * 使用递归方式
     * */
    public static ListNode removeElements4(ListNode head, int val) {
        if (head == null) {
            return null;
        }

        ListNode res = removeElements4(head.next, val);

        if (head.val == val) {
            return res;
        }else{
            head.next = res;
            return head;
        }


    }

 /*
     * 使用递归方式
     * */
    public static ListNode removeElements5(ListNode head, int val) {
        if (head == null) {
            return null;
        }

        head.next = removeElements4(head.next, val);

        return head.val == val ? head.next : head;

    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 6, 3, 4, 5, 6};

        ListNode head = new ListNode(nums);
        System.out.println(head);

        removeElements3(head, 6);
        ListNode res = removeElements4(head, 6);
        System.out.println(res);
    }
}

1.3 递归调用解读

在这里插入图片描述

1.4 递归的打印调试

package linkedlist.digui;

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    public ListNode(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("arr can not be empty!");
        }

        this.val = arr[0];

        ListNode cur = this;

        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();

        ListNode cur = this;
        while (cur != null) {
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("NULL");

        return res.toString();
    }
}


public class RemoveElements {

    /*
     * 使用递归方式
     * */
    public static ListNode removeElements(ListNode head, int val, int depth) {

        String depthString = generateDepthString(depth);

        System.out.print(depthString);
        System.out.println("Call: remove" + val + " in " + head);

        if (head == null) {
            System.out.print(depthString);
            System.out.println("Return: " + head);
            return head;
        }

        ListNode res = removeElements(head.next, val, depth + 1);

        System.out.print(depthString);
        System.out.println("After remove " + val + " : " + res);

        ListNode ret;

        if (head.val == val) {
            ret = res;
        } else {
            head.next = res;
            ret = head;
        }

        System.out.print(depthString);
        System.out.println("Return: " + ret);
        return ret;

    }

    private static String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();

        for (int i = 0; i < depth; i++) {
            res.append("--");
        }

        return res.toString();
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 6, 3, 4, 5, 6};

        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res = removeElements(head, 6,0);
        System.out.println(res);
    }
}

1->2->6->3->4->5->6->NULL
Call: remove6 in 1->2->6->3->4->5->6->NULL
--Call: remove6 in 2->6->3->4->5->6->NULL
----Call: remove6 in 6->3->4->5->6->NULL
------Call: remove6 in 3->4->5->6->NULL
--------Call: remove6 in 4->5->6->NULL
----------Call: remove6 in 5->6->NULL
------------Call: remove6 in 6->NULL
--------------Call: remove6 in null
--------------Return: null
------------After remove 6 : null
------------Return: null
----------After remove 6 : null
----------Return: 5->NULL
--------After remove 6 : 5->NULL
--------Return: 4->5->NULL
------After remove 6 : 4->5->NULL
------Return: 3->4->5->NULL
----After remove 6 : 3->4->5->NULL
----Return: 3->4->5->NULL
--After remove 6 : 3->4->5->NULL
--Return: 2->3->4->5->NULL
After remove 6 : 2->3->4->5->NULL
Return: 1->2->3->4->5->NULL
1->2->3->4->5->NULL

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/86708695