mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update linked list challenge (#124)
Remove extraneous check within the find method. Clean up delete method.
This commit is contained in:
parent
08d86de559
commit
5977fc994d
|
@ -44,8 +44,6 @@ class LinkedList(object):
|
||||||
def find(self, data):
|
def find(self, data):
|
||||||
if data is None:
|
if data is None:
|
||||||
return None
|
return None
|
||||||
if self.head is None:
|
|
||||||
return None
|
|
||||||
curr_node = self.head
|
curr_node = self.head
|
||||||
while curr_node is not None:
|
while curr_node is not None:
|
||||||
if curr_node.data == data:
|
if curr_node.data == data:
|
||||||
|
@ -58,15 +56,17 @@ class LinkedList(object):
|
||||||
return
|
return
|
||||||
if self.head is None:
|
if self.head is None:
|
||||||
return
|
return
|
||||||
|
if self.head.data == data:
|
||||||
|
self.head = None
|
||||||
|
return
|
||||||
prev_node = self.head
|
prev_node = self.head
|
||||||
curr_node = prev_node.next
|
curr_node = self.head.next
|
||||||
while curr_node is not None:
|
while curr_node is not None:
|
||||||
if curr_node.data == data:
|
if curr_node.data == data:
|
||||||
prev_node.next = curr_node.next
|
prev_node.next = curr_node.next
|
||||||
return
|
return
|
||||||
else:
|
prev_node = curr_node
|
||||||
prev_node = curr_node
|
curr_node = curr_node.next
|
||||||
curr_node = curr_node.next
|
|
||||||
|
|
||||||
def delete_alt(self, data):
|
def delete_alt(self, data):
|
||||||
if data is None:
|
if data is None:
|
||||||
|
|
|
@ -231,8 +231,6 @@
|
||||||
" def find(self, data):\n",
|
" def find(self, data):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
" return None\n",
|
" return None\n",
|
||||||
" if self.head is None:\n",
|
|
||||||
" return None\n",
|
|
||||||
" curr_node = self.head\n",
|
" curr_node = self.head\n",
|
||||||
" while curr_node is not None:\n",
|
" while curr_node is not None:\n",
|
||||||
" if curr_node.data == data:\n",
|
" if curr_node.data == data:\n",
|
||||||
|
@ -245,15 +243,17 @@
|
||||||
" return\n",
|
" return\n",
|
||||||
" if self.head is None:\n",
|
" if self.head is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
|
" if self.head.data == data:\n",
|
||||||
|
" self.head = None\n",
|
||||||
|
" return\n",
|
||||||
" prev_node = self.head\n",
|
" prev_node = self.head\n",
|
||||||
" curr_node = prev_node.next\n",
|
" curr_node = self.head.next\n",
|
||||||
" while curr_node is not None:\n",
|
" while curr_node is not None:\n",
|
||||||
" if curr_node.data == data:\n",
|
" if curr_node.data == data:\n",
|
||||||
" prev_node.next = curr_node.next\n",
|
" prev_node.next = curr_node.next\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" else:\n",
|
" prev_node = curr_node\n",
|
||||||
" prev_node = curr_node\n",
|
" curr_node = curr_node.next\n",
|
||||||
" curr_node = curr_node.next\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" def delete_alt(self, data):\n",
|
" def delete_alt(self, data):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user