[linkedin] flatten a doubly multi level linked list

本文介绍了一种算法,用于将包含next和child指针的多级链表扁平化为单级链表。该算法按层级遍历链表,并将每个子链表追加到当前层级的末尾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem From Here: http://www.geeksforgeeks.org/flatten-a-linked-list-with-next-and-child-pointers/
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;
};

The above list should be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3->19->15

Solution:
The problem clearly say that we need to flatten level by level. The idea of solution is, we start from first level, process all nodes one by one, if a node has a child, then we append the child at the end of list, otherwise we don’t do anything. After the first level is processed, all next level nodes will be appended after first level. Same process is followed for the appended nodes.

1) Take "cur" pointer, which will point to head of the fist level of the list
2) Take "tail" pointer, which will point to end of the first level of the list
3) Repeat the below procedure while "curr" is not NULL.
    I) if current node has a child then
    a) append this new child list to the "tail"
        tail->next = cur->child
    b) find the last node of new child list and update "tail"
        tmp = cur->child;
        while (tmp->next != NULL)
            tmp = tmp->next;
        tail = tmp;
    II) move to the next node. i.e. cur = cur->next 

Solution

void flattenList(Node head)
{
    /*Base case*/
    if (head == NULL)
       return;

    Node tmp;

    /* Find tail node of first level linked list */
    Node tail = head;
    while (tail.next != NULL)
        tail = tail.next;

    // One by one traverse through all nodes of first level
    // linked list till we reach the tail node
    Node cur = head;
    while (cur != tail)
    {
        // If current node has a child
        if (cur.child)
        {
            // then append the child at the end of current list
            tail.next = cur.child;

            // and update the tail to new last node
            tmp = cur.child;
            while (tmp.next)
                tmp = tmp.next;
            tail = tmp;
        }

        // Change current node
        cur = cur.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值