Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About linked lists
#1
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.
Reply
#2
remove() cannot work without an argument. You could call remove(17) for example, then imprimir() .
ManoEl likes this post
Reply
#3
(Oct-17-2021, 02:49 PM)Gribouillis Wrote: remove() cannot work without an argument. You could call remove(17) for example, then imprimir() .

thank you Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star --- Python lists and Linked Lists --- sabe 3 2,708 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  How to create a linked list and call it? loves 12 4,538 Nov-22-2020, 03:50 PM
Last Post: loves
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  reference in pop function for linked list oloap 0 1,578 Mar-14-2020, 05:52 PM
Last Post: oloap
  set a new object node in a linked list via reference oloap 2 2,106 Mar-13-2020, 09:45 PM
Last Post: oloap
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,291 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Deleting the first item in linked list dan789 7 4,007 Mar-05-2019, 06:34 PM
Last Post: ichabod801
  Not Able To Delete First Node From Python Linked List ribena1980 8 4,279 Mar-05-2019, 03:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020