<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)430:扁平化多級雙向鏈表

          共 2135字,需瀏覽 5分鐘

           ·

          2021-11-09 14:52

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?扁平化多級雙向鏈表,我們先來看題面:
          https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/

          You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked 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 the example below.
          Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

          多級雙向鏈表中,除了指向下一個節(jié)點和前一個節(jié)點指針之外,它還有一個子鏈表指針,可能指向單獨的雙向鏈表。這些子列表也可能會有一個或多個自己的子項,依此類推,生成多級數(shù)據(jù)結(jié)構(gòu),如下面的示例所示。

          給你位于列表第一級的頭節(jié)點,請你扁平化列表,使所有結(jié)點出現(xiàn)在單級雙鏈表中。

          示例

          解題

          思路 :采用DFS,注釋都寫在代碼里了。

          class?Solution?{
          public:
          ????Node* flatten(Node* head) {
          ????????Node *cur = head, *nextNode = NULL, *prevN = head;
          ????????//所有的指針記得初始化,不要以為后面要付值就不要初始值,怕后面if沒進去
          ????????while(prevN)
          ????????{ //遍歷鏈表
          ????????????if(cur && cur->child) //當前有,且有子鏈
          ????????????{
          ????????????????nextNode = cur->next;//記住上層cur后面的
          ????????????????cur->next = flatten(cur->child);//展平
          ????????????????cur->child->prev = cur;//子鏈頭接上,展平
          ????????????????cur->child = NULL;//將cur孩子刪掉
          ????????????}
          ????????????prevN = cur;//prevnode移動
          ????????????if(cur)
          ????????????????cur = cur->next;//cur移動
          ????????????if(!cur && nextNode)
          ????????????{ //當cur到終點的下一個時,且上層后面還有nextnode
          ????????????????prevN->next = nextNode;//展平的終點prevN指向上層的nextnode
          ????????????????nextNode->prev = prevN;//反向接上
          ????????????????cur = prevN->next;//cur移動
          ????????????????nextNode = NULL;//已接好,暫時沒有child的需要展平,nextNode清空
          ????????????}
          ????????}
          ????????return?head;
          ????}
          };



          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-420題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)421:數(shù)組中兩個數(shù)的最大異或值

          LeetCode刷題實戰(zhàn)422:有效的單詞方塊

          LeetCode刷題實戰(zhàn)423:從英文中重建數(shù)字

          LeetCode刷題實戰(zhàn)424:替換后的最長重復(fù)字符

          LeetCode刷題實戰(zhàn)425:單詞方塊

          LeetCode刷題實戰(zhàn)426:將二叉搜索樹轉(zhuǎn)化為排序的雙向鏈表

          LeetCode刷題實戰(zhàn)427:建立四叉樹

          LeetCode刷題實戰(zhàn)428:序列化和反序列化 N 叉樹

          LeetCode刷題實戰(zhàn)429:N 叉樹的層序遍歷

          瀏覽 45
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩AV中文字幕大香 | 性爱av在线 | 亚洲视频观看免费 | 兔费A片免费看 | 欧洲色图亚洲色图 |