代码拉取完成,页面将自动刷新
/***********************************************************
* @Description : 虚拟头结点可以大大简化代码
* @author : 梁山广(Laing Shan Guang)
* @date : 2019/12/29 14:18
* @email : liangshanguang2@gmail.com
***********************************************************/
package Chapter05Recursion.Section6RecursionDebug;
import Chapter05Recursion.Section1To2LeetCode203.ListNode;
class SolutionDebug {
/**
* 删除链表中值为val的所有元素
*
* @param head 链表的头结点
* @param val 要删除的节点的值
* @param depth 递归的深度
* @return
*/
public ListNode removeElements(ListNode head, int val, int depth) {
// 下面两行是递归进入新层
System.out.print(generateRecursionDepthStr(depth));
System.out.println("第" + depth + "层递归:head=" + head + ",要找的值为节点值为:" + val);
if (head == null) {
return null;
}
head.next = removeElements(head.next, val, depth + 1);
// 下面两行是递归退出当前层
System.out.print(generateRecursionDepthStr(depth));
System.out.println("第" + depth + "层递归:head=" + head + ",要找的值为节点值为:" + val);
// head节点要删除就直接跳过head节点,否则就返回原来的
return head.val == val ? head.next : head;
}
/**
* 生成递归深度的字符串
*
* @param depth 递归层数
* @return depth*2个"--"
*/
private String generateRecursionDepthStr(int depth) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < depth; i++) {
sb.append("--");
}
return sb.toString();
}
public static void main(String[] args) {
int[] nums = {1, 2, 6, 3, 4, 5, 6};
ListNode head = new ListNode(nums);
ListNode result = (new SolutionDebug()).removeElements(head, 6, 1);
System.out.println();
System.out.println("递归结束,结果为::" + result);
}
}
/**
* --第1层递归:head=1->2->6->3->4->5->6->NULL,要找的值为节点值为:6
* ----第2层递归:head=2->6->3->4->5->6->NULL,要找的值为节点值为:6
* ------第3层递归:head=6->3->4->5->6->NULL,要找的值为节点值为:6
* --------第4层递归:head=3->4->5->6->NULL,要找的值为节点值为:6
* ----------第5层递归:head=4->5->6->NULL,要找的值为节点值为:6
* ------------第6层递归:head=5->6->NULL,要找的值为节点值为:6
* --------------第7层递归:head=6->NULL,要找的值为节点值为:6
* ----------------第8层递归:head=null,要找的值为节点值为:6
* --------------第7层递归:head=6->NULL,要找的值为节点值为:6
* ------------第6层递归:head=5->NULL,要找的值为节点值为:6
* ----------第5层递归:head=4->5->NULL,要找的值为节点值为:6
* --------第4层递归:head=3->4->5->NULL,要找的值为节点值为:6
* ------第3层递归:head=6->3->4->5->NULL,要找的值为节点值为:6
* ----第2层递归:head=2->3->4->5->NULL,要找的值为节点值为:6
* --第1层递归:head=1->2->3->4->5->NULL,要找的值为节点值为:6
*
* 递归结束,结果为::1->2->3->4->5->NULL
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。