公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

我们将实现一个函数来删除链表中右侧具有更大值的节点。方法是从右向左遍历链表并跟踪到目前为止遇到的最大值。对于每个节点,我们将其值与最大值进行比较,如果其值小于最大值则删除该节点。这样,右侧所有大于最大值的节点都会被删除。

方法

删除右侧值较大的节点的方法可以分为以下 7 个步骤:

    从头到尾遍历链表。

    跟踪当前节点、前一个节点以及迄今为止看到的最大值。

    如果当前节点的值小于目前看到的最大值,则通过更新前一个节点的 next 指针来删除当前节点。

    将目前看到的最大值更新为当前节点的值。

    将当前节点移动到下一个节点。

    重复步骤 3 到 5,直到到达链表末尾。

    返回更新后的链表的头。

    示例

    给定一个单链表,任务是删除右侧具有更大值的节点。这个想法是从右到左遍历列表并跟踪到目前为止看到的最大值。当我们遍历列表时,我们删除值小于目前看到的最大值的节点。

    这是 JavaScript 中的实现 –

    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;
    }
    

    登录后复制

    说明

      首先,我们创建一个链表类,其中包含 Node 类来定义链表中的每个节点。

      在 LinkedList 类中,我们有一个函数 add() 来将新节点添加到列表中。

      deleteNodes()函数实现删除右侧值较大的节点的逻辑。

      我们从右向左遍历列表,跟踪到目前为止看到的最大值。

      如果当前节点的值大于最大值,我们更新最大值。

      如果当前节点的值小于最大值,我们通过更新前一个节点的 next 引用以指向当前节点的下一个节点来删除该节点。

      最后,如果第一个节点的值小于最大值,我们更新头引用以指向第一个节点的下一个节点。

      删除节点后的链表将只包含值为以下的节点:

      以上就是JavaScript 程序删除右侧具有更大值的节点的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:javascript 删除 更大 程序 节点
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定