日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

我們將實現(xiàn)一個函數(shù)來刪除鏈表中右側(cè)具有更大值的節(jié)點。方法是從右向左遍歷鏈表并跟蹤到目前為止遇到的最大值。對于每個節(jié)點,我們將其值與最大值進行比較,如果其值小于最大值則刪除該節(jié)點。這樣,右側(cè)所有大于最大值的節(jié)點都會被刪除。

方法

刪除右側(cè)值較大的節(jié)點的方法可以分為以下 7 個步驟:

    從頭到尾遍歷鏈表。

    跟蹤當(dāng)前節(jié)點、前一個節(jié)點以及迄今為止看到的最大值。

    如果當(dāng)前節(jié)點的值小于目前看到的最大值,則通過更新前一個節(jié)點的 next 指針來刪除當(dāng)前節(jié)點。

    將目前看到的最大值更新為當(dāng)前節(jié)點的值。

    將當(dāng)前節(jié)點移動到下一個節(jié)點。

    重復(fù)步驟 3 到 5,直到到達鏈表末尾。

    返回更新后的鏈表的頭。

    示例

    給定一個單鏈表,任務(wù)是刪除右側(cè)具有更大值的節(jié)點。這個想法是從右到左遍歷列表并跟蹤到目前為止看到的最大值。當(dāng)我們遍歷列表時,我們刪除值小于目前看到的最大值的節(jié)點。

    這是 JavaScript 中的實現(xiàn) –

    class Node {
      constructor(value) {
        this.value = value;
        this.next = null;
      }
    }
    class LinkedList {
      constructor() {
        this.head = null;
      }
      // Add a new node to the linked list
      add(value) {
        const node = new Node(value);
        if (!this.head) {
          this.head = node;
          return;
        }
        let current = this.head;
        while (current.next) {
          current = current.next;
        }
        current.next = node;
      }
      // Function to delete nodes with greater value on right
      deleteNodes() {
        let prev = null;
        let current = this.head;
        let max = this.head.value;
        // Traverse the linked list from right to left
        while (current.next) {
          // If the current node has a greater value than the max value seen so far
          if (current.next.value > max) {
            max = current.next.value;
            prev = current;
          } else {
            // Delete the node with smaller value
            prev.next = current.next;
          }
          current = current.next;
        }
        // If the last node has a smaller value than the max value seen so far
        if (this.head.value < max) {
          this.head = this.head.next;
        }
      }
    }
    // Test the code
    const linkedList = new LinkedList();
    linkedList.add(12);
    linkedList.add(15);
    linkedList.add(10);
    linkedList.add(11);
    linkedList.add(5);
    linkedList.add(6);
    linkedList.add(2);
    linkedList.add(3);
    linkedList.deleteNodes();
    let current = linkedList.head;
    while (current) {
      console.log(current.value);
      current = current.next;
    }
    

    登錄后復(fù)制

    說明

      首先,我們創(chuàng)建一個鏈表類,其中包含 Node 類來定義鏈表中的每個節(jié)點。

      在 LinkedList 類中,我們有一個函數(shù) add() 來將新節(jié)點添加到列表中。

      deleteNodes()函數(shù)實現(xiàn)刪除右側(cè)值較大的節(jié)點的邏輯。

      我們從右向左遍歷列表,跟蹤到目前為止看到的最大值。

      如果當(dāng)前節(jié)點的值大于最大值,我們更新最大值。

      如果當(dāng)前節(jié)點的值小于最大值,我們通過更新前一個節(jié)點的 next 引用以指向當(dāng)前節(jié)點的下一個節(jié)點來刪除該節(jié)點。

      最后,如果第一個節(jié)點的值小于最大值,我們更新頭引用以指向第一個節(jié)點的下一個節(jié)點。

      刪除節(jié)點后的鏈表將只包含值為以下的節(jié)點:

      以上就是JavaScript 程序刪除右側(cè)具有更大值的節(jié)點的詳細內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!

分享到:
標簽:javascript 刪除 更大 程序 節(jié)點
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定