需要帮助可视化链接列表

时间:2019-01-18 19:19:42

标签: algorithm linked-list visualization

我一直在试图理解单链列表。

设置变量(例如)之间有什么区别

cur=head
prev=head

执行变量curprev吗?和

prev.next =cur.next

影响链表?我如何可视化它?

    cur=head
    prev=head
    c=0
    while(end!=None):
        end=end.next
        c+=1
    print(c)
    mark=c-n
    if mark==0:
        head=head.next    

    while(mark>0):
        prev=cur
        cur=cur.next
        mark-=1
    prev.next=cur.next
    return head

2 个答案:

答案 0 :(得分:0)

将元素(节点)视为盒子。每个框都有两个属性数据和next(桥接到下一个元素)。

每个linkedList都有一个特殊的框,称为head(头),这是您唯一知道的元素(列表的网关),如果需要列表中的其他元素,请遵循桥链。

现在,当您说事物链接到cur = head或prev = head时,您只是在将该网关的副本复制到列表中,这样您就可以通过说出cur = cur.next来遍历列表,并且仍然保留原始网关(head )。

cur = cur.next的作用只是通过桥查看cur的下一个节点,并更新cur以直接指向该节点的下一个节点,就像您穿越新娘一样。

答案 1 :(得分:0)

在整个说明中,我们假设示例列表1->2->3->4->5

此算法从链接列表的末尾删除第n个节点。代码的第一部分只是查找并打印列表的长度(我假设c是“ count”的缩写):

c=0
while(end!=None):
    end=end.next
    c+=1
print(c)

请注意,缺少变量end,它是用于遍历列表的运行节点,必须已像end = head一样进行了初始化。 end遍历列表后,c = 5


让我们遍历样本列表上的主节点删除逻辑。第一部分,

if mark==0:
    head=head.next    

处理一个边缘情况,我们需要删除列表的开头。 n必须等于列表的长度,或者在此示例中为5,这意味着我们要删除倒数第5个节点。在这里,只需将head设置为其下一个元素,即可删除对1节点的所有引用。该操作完成后,将在某个时间点对其进行垃圾收集。

before:
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
    ^    
    |    
   head
after:
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
                ^    
                |    
               head
the resulting list:
+--------+  +--------+  +--------+  +--------+
| val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+
    ^    
    |    
   head

关于

中所述的典型情况
while(mark>0):
    prev=cur
    cur=cur.next
    mark-=1
prev.next=cur.next

要删除的元素不是头部,让我们来看一个n = 2的示例。在这种情况下,我们要从1->2->3->4->54中删除倒数第二个节点。

while循环开始之前,prevcurhead指向以下内容:

   head
     |
     v
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
  ^    ^
  |    |
prev  cur

在第一次迭代中,prev设置为cur

+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
  ^    ^
  |    |
 prev cur

mark = 3

然后,cur设置为其下一个节点:

+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
    ^            ^
    |            |
   prev         cur

mark = 3

同一件事再做两次:

+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
              ^    ^
              |    |
            prev  cur

mark = 2
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
                 ^            ^
                 |            |
                prev         cur

mark = 2
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
                          ^    ^
                          |    |
                         prev cur

mark = 1
+--------+  +--------+  +--------+  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  +--------+
                            ^            ^
                            |            |
                           prev         cur

mark = 1

这时,循环中断,因为mark递减为0。您可以看到我们的节点处于最佳位置,可以将4与列表prev.next=cur.next断开链接。让我们开始吧:

                                   +------------------+
                                   |                  |
                                   |                  v
+--------+  +--------+  +--------+ |  +--------+  +--------+
| val: 1 |  | val: 2 |  | val: 3 | |  | val: 4 |  | val: 5 |
| next: --->| next: --->| next: ---+  | next: --->| next: ---> [None]
+--------+  +--------+  +--------+    +--------+  +--------+
                            ^              ^ 
                            |              |
                           prev           cur

cur指向值为4的节点不可访问,并且没有引用该节点的内容。将来某个时候,解释器会收集垃圾。现在,它已不再是列表的一部分,当代码完成时,我们将得到以下结果:

+--------+  +--------+  +--------+  +--------+ 
| val: 1 |  | val: 2 |  | val: 3 |  | val: 5 |  
| next: --->| next: --->| next: --->| next: ---> [None]
+--------+  +--------+  +--------+  +--------+  
     ^
     |
   head