<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      劍指offer-25、復雜鏈表的復制

      題?描述

      輸??個復雜鏈表(每個節(jié)點中有節(jié)點值,以及兩個指針,?個指向下?個節(jié)點,另?個特殊指針random 指向?個隨機節(jié)點),請對此鏈表進?深拷?,并返回拷?后的頭結點。(注意,輸出結果中請不要返回參數中的節(jié)點引?,否則判題程序會直接返回空)

      思路及解答

      哈希表映射

      使用哈希表存儲原節(jié)點和新節(jié)點的映射關系:

      1. 第一次遍歷:創(chuàng)建所有新節(jié)點,并建立原節(jié)點到新節(jié)點的映射
      2. 第二次遍歷:根據映射關系設置新節(jié)點的nextrandom指針
      public class Solution {
          public Node copyRandomList(Node head) {
              if (head == null) {
                  return null;
              }
              
              // 創(chuàng)建哈希表存儲原節(jié)點到新節(jié)點的映射
              HashMap<Node, Node> map = new HashMap<>();
              Node current = head;
              
              // 第一次遍歷:創(chuàng)建所有新節(jié)點并建立映射
              while (current != null) {
                  map.put(current, new Node(current.val));
                  current = current.next;
              }
              
              // 第二次遍歷:設置新節(jié)點的next和random指針
              current = head;
              while (current != null) {
                  Node newNode = map.get(current);
                  newNode.next = map.get(current.next);
                  newNode.random = map.get(current.random);
                  current = current.next;
              }
              
              return map.get(head);
          }
      }
      
      class Node {
          int val;
          Node next;
          Node random;
          
          public Node(int val) {
              this.val = val;
              this.next = null;
              this.random = null;
          }
      }
      
      • ?時間復雜度?:O(n),兩次遍歷鏈表
      • ?空間復雜度?:O(n),需要存儲所有節(jié)點的映射關系

      節(jié)點插入拆分法

      通過在原鏈表中插入新節(jié)點來避免使用額外空間:

      1. ?節(jié)點復制插入?:在每個原節(jié)點后面插入一個復制的新節(jié)點
      2. ?設置random指針?:新節(jié)點的random指向原節(jié)點random的下一個節(jié)點
      3. ?鏈表拆分?:將混合鏈表拆分為原鏈表和新鏈表
      public class Solution {
          public Node copyRandomList(Node head) {
              if (head == null) {
                  return null;
              }
              
              // 第一步:在每個節(jié)點后面插入復制的節(jié)點
              Node current = head;
              while (current != null) {
                  Node newNode = new Node(current.val);
                  newNode.next = current.next;
                  current.next = newNode;
                  current = newNode.next;
              }
              
              // 第二步:設置復制節(jié)點的random指針
              current = head;
              while (current != null) {
                  if (current.random != null) {
                      current.next.random = current.random.next;
                  }
                  current = current.next.next;
              }
              
              // 第三步:拆分鏈表
              Node newHead = head.next;
              current = head;
              while (current != null) {
                  Node temp = current.next;
                  current.next = temp.next;
                  if (temp.next != null) {
                      temp.next = temp.next.next;
                  }
                  current = current.next;
              }
              
              return newHead;
          }
      }
      
      • ?時間復雜度?:O(n),三次遍歷鏈表
      • ?空間復雜度?:O(1),只使用固定數量的指針變量
      posted @ 2025-08-27 09:00  程序員Seven  閱讀(47)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 邵东县| 深夜福利啪啪片| 日韩中文字幕国产精品| julia无码中文字幕一区| 国产精品国产三级国AV| 亚洲精品中文av在线| 国产精品老熟女露脸视频| 中文字幕乱码一区二区免费| 四虎永久精品在线视频| 国产成人8X人网站视频| 亚洲人成网站免费播放| 一区二区中文字幕久久| 久久国产精品不只是精品| 欧美日本在线一区二区三区| 天堂网亚洲综合在线| 日本道高清一区二区三区| 国产精品v片在线观看不卡| 国产成人午夜精品福利| 91一区二区三区蜜桃臀| 亚洲国产成人久久一区久久| 在线无码免费的毛片视频| 无码国产精品成人| 天天躁日日躁狠狠躁2018| 五月婷婷久久草| 婷婷成人丁香五月综合激情| 久久中文字幕一区二区| 国产激情福利短视频在线| 欧美伦费免费全部午夜最新| 成人一区二区不卡国产| 日日噜噜夜夜狠狠久久蜜桃| 在线国产你懂的| 露脸国产精品自产拍在线观看| 香港日本三级亚洲三级| 精品综合一区二区三区四区| 少妇高潮惨叫喷水在线观看| 国产精品国产三级在线专区| 免费人成视频在线视频电影| 亚洲人妻精品中文字幕| 亚洲一二区在线视频播放| 在线成人精品国产区免费| 在线观看中文字幕国产码|