Python Forum
Deleting the first item in linked list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting the first item in linked list
#1
Hello, this is what I have so far:

class Linked_list:
    def __init__(self, data, next=None):
        self.data, self.next = data, next

def print_list(list):
    while list is not None:
        print(list.data, end=' ---> ')
        list = list.next
    print(None)

def make(sequence):
    list = None
    for value in reversed(sequence):
        list = Linked_list(value, list)
    return list

def remove_first(list):
    ...
I simply donĀ“t know how to delete the first item of my linked list. I should use just structure I wrote above. Can you give me a help?

Thanks.
Reply
#2
Using list as a variable name is a bad idea. list is a built-in, and by using it as a variable name, you block the use of the built-in, which could cause errors in other code.

Let's say your linked list is called llist. llist is actually the first item in you list, and it links to the next item in the list. The second item in the list is llist.next. Can you see then how to make the first item in your list be the second item in your list, thus deleting the previous first item?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I could change a refference to the second item? But not sure how to do this using a separate function remove_first.
Reply
#4
If remove_first is not supposed to have a return value, you could copy the values of the second item in the list into the first item of the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Something like this?

llist.data = llist.next.data
Reply
#6
That's half of it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
llist.next = llist.next.next works at least in my case. Is it correct?
Reply
#8
Yeah, that's the other half.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Finding string in list item jesse68 8 1,798 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to easily create a list of already existing item CompleteNewb 15 3,379 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,548 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  About linked lists ManoEl 2 1,570 Oct-17-2021, 03:21 PM
Last Post: ManoEl
  deleting select items from a list Skaperen 13 4,390 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  count item in list korenron 8 3,372 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,846 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 2,324 Jan-06-2021, 10:40 PM
Last Post: arielma
  Deleting employee from list SephMon 3 3,199 Jan-05-2021, 04:15 AM
Last Post: deanhystad
  How to create a linked list and call it? loves 12 4,356 Nov-22-2020, 03:50 PM
Last Post: loves

Forum Jump:

User Panel Messages

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