0 课程地址
https://coding.imooc.com/lesson/207.html#mid=13442
1 重点关注
1.1 递归算法的调试打印日志层级调试
参考3.1
2 课程内容
3 Coding
3.1 递归算法的调试打印日志层级调试
- 调试类:
package com.company; /** * 使用递归实现 * 203问题解决 删除链表的元素 * @author weidoudou * @date 2022/11/1 10:47 **/ class Solution3 { public ListNode removeElements(ListNode head, int val,int depth) { //1 基本方法 if(null==head){ return null; } for(int i = 0;i<depth;i++){ System.out.print("=="); } System.out.println(head); depth = depth+1; //2 方法拆分 ListNode cur = removeElements(head.next,val,depth); if(head.val==val){ for(int i = 0;i<depth;i++){ System.out.print("=="); } System.out.println(cur); return cur; }else{ head.next = cur; for(int i = 0;i<depth;i++){ System.out.print("=="); } System.out.println(head); return head; } } public static void main(String[] args) { int[] nums = {1,2,6,3,4,5,6}; ListNode listNode = new ListNode(nums); //System.out.println(listNode); ListNode res = new Solution3().removeElements(listNode,6,0); System.out.println(res); } }
- 测试结果:
1->2->6->3->4->5->6->Null ==2->6->3->4->5->6->Null ====6->3->4->5->6->Null ======3->4->5->6->Null ========4->5->6->Null ==========5->6->Null ============6->Null ==============null ============5->Null ==========4->5->Null ========3->4->5->Null ======3->4->5->Null ====2->3->4->5->Null ==1->2->3->4->5->Null 1->2->3->4->5->Null Process finished with exit code 0
诸葛