-
-
Save adarsh0806/02d8e1d54d510294e75dfbc0d9bd7059 to your computer and use it in GitHub Desktop.
def delete_first(self): | |
deleted = self.head | |
if self.head: | |
self.head = self.head.next | |
deleted.next = None | |
return deleted |
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
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
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
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😊
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
def delete_first(self):
elementToRemove = None
if(self.head != None ):
elementToRemove = self.head
self.head = self.head.next
return elementToRemove
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
def delete_first(self):
current = self.head
if self.head:
self.head=self.head.next
return current
def delete_first(self):
if not self.head:
return None
else:
x = self.head
self.head = self.head.next
return x
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
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
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
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
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.
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
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```
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
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
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?