【问题标题】:Understanding LinkedLists References了解 LinkedLists 参考
【发布时间】:2021-05-07 01:30:11
【问题描述】:

我一直在努力理解链表,并且阅读了很多关于它们的(教程/文章等),但不能完全掌握它。我正在尝试在这里解决leetcode problem。这是问题陈述:

给定排序链表的头部,删除所有重复项,使每个元素只出现一次。返回排序好的链表。

在下面的特定示例解决方案中,var current 只是对 head 中的节点/节点的引用吗?

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */

var deleteDuplicates = function(head) {
    var current = head;
    
    while(current) {
        if(current.next !== null && current.val == current.next.val) {
            current.next = current.next.next;
        } else {
            current = current.next;
        }
    }
    
    return head;
};

那么循环中所做的更改实际上是在修改head中的节点?

我一直在纠结于电流等于磁头的想法,然后对循环中的电流进行了更改,然后无法理解磁头是如何被更改的。

【问题讨论】:

    标签: javascript linked-list singly-linked-list


    【解决方案1】:

    一开始currenthead指向链表的同一个指针。一旦电流移动current = current.nextcurrent.next = current.next.nextcurrenthead 将不再指向同一个链表节点。在这里,我试图通过一个简单的例子来展示这个过程的步骤:

    我希望这些数字能帮助您了解headcurrent 在此处扮演不同的角色。

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 2013-05-10
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2021-06-04
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多