Skip to content

Instantly share code, notes, and snippets.

@adarsh0806
Created May 31, 2016 17:35
Show Gist options
  • 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
@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