Skip to content

Instantly share code, notes, and snippets.

@adarsh0806
Created May 31, 2016 17:35
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save adarsh0806/02d8e1d54d510294e75dfbc0d9bd7059 to your computer and use it in GitHub Desktop.
Save adarsh0806/02d8e1d54d510294e75dfbc0d9bd7059 to your computer and use it in GitHub Desktop.
delete_first
def delete_first(self):
deleted = self.head
if self.head:
self.head = self.head.next
deleted.next = None
return deleted
@rchatti
Copy link

rchatti commented Jun 8, 2019

Echoing @ScottDWebster... why "return" anything?
Is that Garbage Collection in Python?

@rchatti
Copy link

rchatti commented Jun 8, 2019

@ScottDWebster
Here is the reason you "Return" the value of the Deleted Element: Its to implement POP method, if you are implementing Stacks.
Along with Deleting the Head, POP is also supposed to return its value... hence the "return" statement. Its "future-proofing" your "delete-first" to support 'POP' functionality.

@anilhanumanthu
Copy link

def delete_first(self):
if self.head:
deleted_item = self.head
self.head = deleted_item.next
deleted_item.next = None
return deleted_element
else:
return None

@mhepeyiler
Copy link

mhepeyiler commented Oct 26, 2019

def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"
        deleted = self.head
        self.head = self.head.next if self.head else None
        return deleted

It is the same with the first answer. But the if statement much smaller.

@epsi95
Copy link

epsi95 commented Mar 25, 2020

In that code:

def delete_first(self):
    deleted = self.head
    if self.head:
        self.head = self.head.next
        deleted.next = None
    return deleted

is the deleted.next = None line necessary? Is it required for python garbage collector?
Also, I have a doubt that whenever we are pushing or poping, we are not actually deleting something, is it efficient or python garbage collector smart enough to reuse them

@plicatibu
Copy link

@epsi95 by making deleted.next = None you prevent the possibility to someone iterate / modify the remaining elements of the stack.

@epsi95
Copy link

epsi95 commented Mar 27, 2020

@plicatibu ya, you are right, actually, I didn't think from user point of view.

@Redick0x7E1
Copy link

This works well. But from Udacity perspective. It doesn't specify you need to return anything on the pop command. It can just work without any return right?

@hemantghuge
Copy link

Another way of writing it.


if self.head:
    deleted_element = self.head
    self.head = deleted_element.next
    deleted_element.next = None
    return deleted_element
else:
    return self.head

@svrakata
Copy link

Another way of writing it.


if self.head:
    deleted_element = self.head
    self.head = deleted_element.next
    deleted_element.next = None
    return deleted_element
else:
    return self.head
`

why there is no function please tell

he's pasting just the body of the function it's not a solution without function definition. :P

@Lawrence-Krukrubo
Copy link

Lawrence-Krukrubo commented Sep 24, 2020

Another sweet and succinct way is:-

def delete_first(self):
    "Delete the first (head) element in the LinkedList and return it"

    current = self.head
    if current:
        self.head = current.next
    return current

@parvinderandroid
Copy link

This is another way to achieve the same result!

    def delete_first(self):
        elementToRemove = None
        if(self.head != None ):
            elementToRemove = self.head
            self.head = self.head.next
        return elementToRemove

Did not expect to see you here😊

@Lawrence-Krukrubo
Copy link

@ishannd
Copy link

ishannd commented May 14, 2021

Does this not work?

def delete_first(self):
"Delete the first (head) element in the LinkedList as return it"
if self.head:
head_ele = self.head
self.head = head_ele.next
head_ele.next = None
return head_ele.value
else:
return None

@Vais-svg
Copy link

Vais-svg commented Jun 4, 2021

def delete_first(self):

    elementToRemove = None
    if(self.head != None ):
        elementToRemove = self.head
        self.head = self.head.next
    return elementToRemove

@IamPhytan
Copy link

Not memory efficient, since another Element is instantiated, yet it takes less lines

def delete_first(self):
    head_element = Element(self.head.value)
    if self.head is not None:
        self.head = self.head.next
    return head_element

@AbhinavSingh111
Copy link

def delete_first(self):
        current = self.head
        if self.head:
            self.head=self.head.next
            return current

@dee-vy
Copy link

dee-vy commented Jul 22, 2022

def delete_first(self):
    if not self.head:
        return None
    else:
        x = self.head
        self.head = self.head.next
        return x

@shinnida220
Copy link

shinnida220 commented Oct 5, 2022

Here's what I wrote

def delete_first(self):
     "Delete the first (head) element in the LinkedList as return it"
      current = self.head
      if current and current.next != None:
          self.head = current.next
      else:
         self.head = None
      return current

@SelfTaught-HamzaCodes
Copy link

This is how I implemented it :)

    def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"

        if not self.head:
            return None

        if self.head:
            deleted = self.head
            self.head = self.head.next
            return deleted.value

@ChandanChainani
Copy link

ChandanChainani commented Mar 26, 2023

Here's another version

    def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"
        self.head, node = (self.head.next, Element(self.head.value)) if self.head else (None, None)
        return node

@yaakash @karthik19894 I think it will be automatically garbage collected, If we see logically since when self.head is returned assigning to deleted variable the reference counter will be decreased after returning from delete_first if I am not wrong the reference counter would be 0 if the return value is not reassigned then garbage collector knows that if can clean the data since nothing is pointing to it.

def delete_first(self):
    deleted = self.head
    if self.head:
        self.head = self.head.next
        deleted.next = None
    return deleted

@pranabsarma18
Copy link

pranabsarma18 commented Apr 20, 2023

My implementation:

def delete_first(self):
    "Delete the first (head) element in the LinkedList as return it"
    if self.head and self.head.next:
        deleted_element = self.head
        self.head = self.head.next
        return deleted_element
    elif self.head and self.head.next == None:
        deleted_element = self.head
        self.head = None
        return deleted_element
    else:
        return None

@AlirezaShk
Copy link

My implementation:

def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"
        node = self.head
        try: self.head = self.head.next
        except AttributeError: pass
        finally: return node

This is according to the official best practices of Python, adhering to the EAFP. Basically claiming that it's computationally faster (in no-error cases) to use try/except instead of if/else statements.

@Kingkobi01
Copy link

My Implementation:

 def delete_first(self):
        "Delete the first (head) element in the LinkedList as return it"
        if self.head is None:
            return None
            
        current = self.head
        self.head = self.head.next
        
        return current

@demoreon
Copy link

I hope this helps

        "Delete the first (head) element in the LinkedList as return it"
        temp = self.head
        self.head = temp.next if temp else None
        return temp```

@Tadei18
Copy link

Tadei18 commented Jul 13, 2023

delete
Hope this helps

@LekeneCedric
Copy link

LekeneCedric commented Nov 29, 2023

def delete_first(self):
        deleted_element = None
        if self.head is not None:
            deleted_element = self.head
            deleted_element.next = None
            if self.head.next is not None:
                self.head = self.head.next
            else:
                self.head = None
        return deleted_element

@israel-oye
Copy link

Compared to most solution, I think I was too explicit

def delete_first(self):
        if self.head:
            old_head = self.head
            if self.head.next:
                new_head = self.head.next
                self.head = new_head
                old_head.next = None
            else:
                self.head = None
            return old_head
        return None

@LekeneCedric
Copy link

LekeneCedric commented Jan 18, 2024 via email

@GrvExplorer
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment