Oct-17-2021, 02:22 PM
I'm learning Python and would like help to know how I can remove a node from a linked list from a value, currently my code looks like this:
noRaiz = None
def novoNo(valor):
return {
"valor": valor,
"proximo": None
}
def remove(valor):
global noRaiz
if noRaiz is None:
return
noAtual = noRaiz
if noRaiz["valor"] == valor:
noRaiz = noRaiz["proximo"]
return
while noAtual["proximo"] is not None:
if noAtual["proximo"]["valor"] == valor:
noAtual["proximo"] = noAtual["proximo"]["proximo"]
noAtual = noAtual["proximo"]
def imprimir():
noAtual = noRaiz
while noAtual is not None:
print(noAtual["valor"])
noAtual = noAtual["proximo"]
noRaiz = novoNo(54)
no2 = novoNo(26)
no3 = novoNo(93)
no4 = novoNo(17)
no5 = novoNo(77)
no6 = novoNo(31)
noRaiz["proximo"] = no2
no2["proximo"] = no3
no3["proximo"] = no4
no4["proximo"] = no5
no5["proximo"] = no6
no6["proximo"] = None
imprimir()
remove()noRaiz is the first node on the list, noAtual is the current node in the list at print time when running the code and imprimir() it's the function to print the nodes. How do I remove the number 54 or 17? I did the remove function but it's probably not right because it doesn't remove.
