Python 链表 指针重载 简单逻辑图解

class ToNext:
    def __init__(self, x):
        self.val = x
        self.next = None
prenode = ToNext(0)

Pre { .val 0 .next None \begin{aligned} {\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ \text{.next} \to &{\color{green}\text{None}} \end{cases} \end{aligned}

lastnode = prenode

Last Pre { .val 0 .next None \begin{aligned}{\color{red}\boxed{\text{Last}}} \to {\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ \text{.next} \to &{\color{green}\text{None}} \end{cases} \end{aligned}

lastnode.next = ToNext(8)

Last Pre { .val 0 .next { .val 8 .next None \begin{aligned}{\color{red}\boxed{\text{Last}}} \to {\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ {\color{red}\text{.next}} \to &\begin{cases}\text{.val} \to &8 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases} \end{cases} \end{aligned}

lastnode = lastnode.next

Pre { .val 0 Last { .val 8 .next None \begin{aligned}{\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ {\color{red}\boxed{\text{Last}}} \to &\begin{cases}\text{.val} \to &8 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases} \end{cases} \end{aligned}

lastnode.next = ToNext(5)

Pre { .val 0 Last { .val 8 .next { .val 5 .next None \begin{aligned}{\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ {\color{red}\boxed{\text{Last}}} \to &\begin{cases}\text{.val} \to &8 \\ {\color{red}\text{.next}} \to &\begin{cases}\text{.val} \to &5 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases}\end{cases} \end{cases} \end{aligned}

lastnode = lastnode.next

Pre { .val 0 .next { .val 8 Last { .val 5 .next None \begin{aligned}{\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ \text{.next} \to &\begin{cases}\text{.val} \to &8 \\ {\color{red}\boxed{\text{Last}}} \to &\begin{cases}\text{.val} \to &5 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases}\end{cases} \end{cases} \end{aligned}

output = prenode.next

Pre { .val 0 .next { .val 8 Last { .val 5 .next None \begin{aligned}{\color{blue}\boxed{\text{Pre}}} \to \begin{cases}\text{.val} \to &0 \\ {\color{blue}\text{.next}} \to &\begin{cases}\text{.val} \to &8 \\ {\color{red}\boxed{\text{Last}}} \to &\begin{cases}\text{.val} \to &5 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases}\end{cases} \end{cases} \end{aligned}

Output { .val 8 .next { .val 5 .next None \begin{aligned}{\color{blue}\boxed{\text{Output}}}\to \begin{cases}\text{.val} \to & 8 \\ \text{.next}\to &\begin{cases}\text{.val} \to &5 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases} \end{cases} \end{aligned}

def generateNext(l: list) -> ToNext:
    prenode = ToNext(0)
    lastnode = prenode
    for val in l:
        lastnode.next = ToNext(val)
        lastnode = lastnode.next
    return prenode.next
output = generateNext([1,2,3])

Output { .val 1 .next { .val 2 .next { .val 3 .next None \begin{aligned}{\color{blue}\boxed{\text{Output}}}\to \begin{cases}\text{.val} \to & 1 \\ \text{.next}\to &\begin{cases}\text{.val} \to &2 \\ \text{.next} \to &\begin{cases}\text{.val} \to &3 \\ \text{.next} \to &{\color{green}\text{None}}\end{cases}\end{cases} \end{cases} \end{aligned}

发布了15 篇原创文章 · 获赞 6 · 访问量 1142

猜你喜欢

转载自blog.csdn.net/Varalpha/article/details/104997416