由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道面试题:Flatten a multilevel linked list
相关主题
一道面试题回馈本版,新鲜店面,新题新气象
List Flattening from book L 家面试高频题, 怎么解
LinkedIn Onsite 面经请教一个二叉树镜像问题
又面了一上午,M家的,大家进来做题刚刚电面bloomberg,被问到一个没看到过的问题
分享:non-recursive breadth first search and depth first search algorithm in C那个skiplist的题谁来给谢谢
请教一道面试题合并两个排序好的链表, 优解?
求教一道面试题G家intern电面新鲜面经
Print a binary tree in level order but starting from leaf node up to root版上看到的几道F家的题目
相关话题的讨论汇总
话题: cur话题: struct话题: list话题: null话题: level
进入JobHunting版参与讨论
1 (共1页)
x*****0
发帖数: 452
1
Given a linked list where in addition to the next pointer, each node has a
child pointer, which may or may not point to a separate list. These child
lists may have one or more children of their own, and so on, to produce a
multilevel data structure, as shown in below figure.You are given the head
of the first level of the list. Flatten the list so that all the nodes
appear in a single-level linked list. You need to flatten the list in way
that all nodes at first level should come first, then nodes of second level,
and so on.
Each node is a C struct with the following definition.
struct list
{
int data;
struct list *next;
struct list *child;
};
My idea:
BFS. When encountering a child, push it to a queue. After traversal the
first level, pop an element from the queue and link the last element of the
first level to the popped element.
The following is my code:
void flattenList(struct node *head)
{
struct node* cur = head;
struct node* tail = NULL;
struct node* temp = NULL;
queue q;

while(!q.empty() || cur != NULL)
{
if(cur != NULL)
{
if(!q.empty())
{
temp = q.front();
if(temp == cur)
{
q.pop();
}
}
if(cur->child != NULL)
{
q.push(cur->child);
}
tail = cur;
cur = cur->next;
}
else
{
if(!q.empty())
{
temp = q.front();
q.pop();
tail->next = temp;
cur = tail->next;
}
}
}
}
For the given example:
1 level:10, 5, 12, 7, 11
2 level: 4, 20,13, 17,16
Can anyone help me verify my algorithm?
And really appreciate provide another solution?
Thanks,
Zhong
l*****a
发帖数: 14598
2
please read PIE

level,

【在 x*****0 的大作中提到】
: Given a linked list where in addition to the next pointer, each node has a
: child pointer, which may or may not point to a separate list. These child
: lists may have one or more children of their own, and so on, to produce a
: multilevel data structure, as shown in below figure.You are given the head
: of the first level of the list. Flatten the list so that all the nodes
: appear in a single-level linked list. You need to flatten the list in way
: that all nodes at first level should come first, then nodes of second level,
: and so on.
: Each node is a C struct with the following definition.
: struct list

p*****p
发帖数: 379
3
给个这个例子应该的结果
是4, 20, 13, 17, 6算一层还是前三个一组后两个一组?
另外tail名字误导人……
x*****0
发帖数: 452
4
thanks, got it.
PIE ==> programming interview exposed?

【在 l*****a 的大作中提到】
: please read PIE
:
: level,

x*****0
发帖数: 452
5
4, 20, 13, 17, 6 一层

【在 p*****p 的大作中提到】
: 给个这个例子应该的结果
: 是4, 20, 13, 17, 6算一层还是前三个一组后两个一组?
: 另外tail名字误导人……

1 (共1页)
进入JobHunting版参与讨论
相关主题
版上看到的几道F家的题目分享:non-recursive breadth first search and depth first search algorithm in C
感觉今天结结实实被烙印阴了请教一道面试题
一个GOOG的二叉树面试题求教一道面试题
我恨iPhone@Facebook电面Print a binary tree in level order but starting from leaf node up to root
一道面试题回馈本版,新鲜店面,新题新气象
List Flattening from book L 家面试高频题, 怎么解
LinkedIn Onsite 面经请教一个二叉树镜像问题
又面了一上午,M家的,大家进来做题刚刚电面bloomberg,被问到一个没看到过的问题
相关话题的讨论汇总
话题: cur话题: struct话题: list话题: null话题: level