你如何复制一个链表到另一个目录? [英] How do you copy a linked list into another list?
本文介绍了你如何复制一个链表到另一个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我学习数据结构和链表,但我没有得到如何使链表副本的概念。有人可以解释这一点,可能使用伪code或C code?
I'm studying data structures and linked lists, but I'm not getting the concept of how to make a copy of a linked list. Can someone explain this, possibly using pseudocode or C code?
推荐答案
有关复制一个链表的逻辑是递归的并基于以下意见:
The logic for duplicating a linked list is recursive and based on the following observations:
- 空单的克隆是空表。
- 在与第一个节点x和剩余的节点XS列表的克隆是第X prepended副本XS的克隆。
如果您连接code链表在C ++中,这可能是非常干净的:
If you encode the linked list in C++, this can be very clean:
struct Node {
int value;
Node* next;
};
Node* Clone(Node* list) {
if (list == NULL) return NULL;
Node* result = new Node;
result->value = list->value;
result->next = Clone(list->next);
return result;
}
这篇关于你如何复制一个链表到另一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文