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} Pre→{.val→.next→0None
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} Last→Pre→{.val→.next→0None
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} Last→Pre→⎩⎪⎨⎪⎧.val→.next→0{.val→.next→8None
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} Pre→⎩⎪⎨⎪⎧.val→Last→0{.val→.next→8None
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} Pre→⎩⎪⎪⎪⎨⎪⎪⎪⎧.val→Last→0⎩⎪⎨⎪⎧.val→.next→8{.val→.next→5None
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} Pre→⎩⎪⎪⎪⎨⎪⎪⎪⎧.val→.next→0⎩⎪⎨⎪⎧.val→Last→8{.val→.next→5None
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} Pre→⎩⎪⎪⎪⎨⎪⎪⎪⎧.val→.next→0⎩⎪⎨⎪⎧.val→Last→8{.val→.next→5None
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} Output→⎩⎪⎨⎪⎧.val→.next→8{.val→.next→5None
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} Output→⎩⎪⎪⎪⎨⎪⎪⎪⎧.val→.next→1⎩⎪⎨⎪⎧.val→.next→2{.val→.next→3None