【发布时间】:2018-05-15 23:17:39
【问题描述】:
我是数据结构和递归概念的新手。我很难理解他为什么以及谁能够在这个概念中使用递归。我在论坛中找到了此代码,但我无法真正理解它的概念。对于 2 1 3 4 的简单情况,如果有人能解释迭代步骤,我将不胜感激。
这是黑客等级的链接: https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
Node SortedInsert(Node head,int data) {
Node n = new Node();
n.data = data;
if (head == null) {
return n;
}
else if (data <= head.data) {
n.next = head;
head.prev = n;
return n;
}
else {
Node rest = SortedInsert(head.next, data);
head.next = rest;
rest.prev = head;
return head;
}
}
【问题讨论】:
-
从你的大脑中删除这段代码,永远不要重新访问它。这会像疯了似的泄漏,并有可能在长列表中出现堆栈溢出。
-
谢谢阿德里安。感谢您的回复。
标签: algorithm recursion data-structures doubly-linked-list insertion