【发布时间】:2010-12-20 07:55:42
【问题描述】:
给定一个链表结构,其中每个节点代表一个链表,并且 包含其类型的两个指针:
(i) 指向主列表中下一个节点的指针。
(ii) 指向此节点为头的链表的指针。
编写一个 C 函数将列表展平为单个链表。
例如。
如果给定的链表是
1 -- 5 -- 7 -- 10
| | |
2 6 8
| |
3 9
|
4
然后转换成
1 - 2 - 3 - 4 - 5 - 6 - 9 - 7 - 8 -10
我的解决方案
struct node {
int data;
struct node *fwd; //pointer to next node in the main list.
struct node *down; //pointer to a linked list where this node is head.
}*head,*temp,*temp2;
temp=head;
while(temp->fwd!=NULL) {
temp2=temp->fwd;
while(temp->down!=NULL) {
temp=temp->down;
}
temp->down=temp2;
temp->fwd=NULL;
temp=temp2;
}
如有任何问题请通知我...欢迎其他解决方案和优化
【问题讨论】:
-
我的解决方案:struct node { int data;结构节点 *fwd; //指向主列表中下一个节点的指针。结构节点 *down; //指向此节点为头的链表的指针。 }*head,*temp,*temp2;温度=头;而(temp->fwd!=NULL){ temp2=temp->fwd; while(temp->down!=NULL) { temp=temp->down; } temp->down=temp2;临时->fwd=NULL;温度=温度2;如果有任何事情请通知我...欢迎其他解决方案和优化
-
@wilhelmtell:从它的写作方式猜测你是对的。
-
不,这不是家庭作业...这是一个面试问题。请您提出更好的答案..
-
@sjngm 它缺少最后一部分:“或者你得到一个 D”
-
@prp 列表旅行对于一个好的面试问题来说似乎太天真了
标签: c algorithm linked-list