Skip to content

Instantly share code, notes, and snippets.

@adarsh0806
Created May 31, 2016 17:35
Show Gist options
  • Select an option

  • Save adarsh0806/02d8e1d54d510294e75dfbc0d9bd7059 to your computer and use it in GitHub Desktop.

Select an option

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
@Redick0x7E1

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

Lawrence-Krukrubo commented Sep 24, 2020

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

@ishannd

ishannd commented May 14, 2021

Copy link
Copy Markdown

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

Vais-svg commented Jun 4, 2021

Copy link
Copy Markdown

def delete_first(self):

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

@IamPhytan

Copy link
Copy Markdown

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
Copy Markdown
def delete_first(self):
        current = self.head
        if self.head:
            self.head=self.head.next
            return current

@dee-vy

dee-vy commented Jul 22, 2022

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

@shinnida220

shinnida220 commented Oct 5, 2022

Copy link
Copy Markdown

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
Copy Markdown

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

ChandanChainani commented Mar 26, 2023

Copy link
Copy Markdown

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

pranabsarma18 commented Apr 20, 2023

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

Tadei18 commented Jul 13, 2023

Copy link
Copy Markdown

delete
Hope this helps

@LekeneCedric

LekeneCedric commented Nov 29, 2023

Copy link
Copy Markdown
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
Copy Markdown

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

LekeneCedric commented Jan 18, 2024 via email

Copy link
Copy Markdown

@GrvExplorer

Copy link
Copy Markdown

image

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