From 5977fc994d2dce541b508c5efccba79c955c6bd1 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 27 Nov 2016 08:43:48 -0500 Subject: [PATCH] Update linked list challenge (#124) Remove extraneous check within the find method. Clean up delete method. --- linked_lists/linked_list/linked_list.py | 12 ++++++------ linked_lists/linked_list/linked_list_solution.ipynb | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/linked_lists/linked_list/linked_list.py b/linked_lists/linked_list/linked_list.py index 13d76bf..18d23e6 100644 --- a/linked_lists/linked_list/linked_list.py +++ b/linked_lists/linked_list/linked_list.py @@ -44,8 +44,6 @@ class LinkedList(object): def find(self, data): if data is None: return None - if self.head is None: - return None curr_node = self.head while curr_node is not None: if curr_node.data == data: @@ -58,15 +56,17 @@ class LinkedList(object): return if self.head is None: return + if self.head.data == data: + self.head = None + return prev_node = self.head - curr_node = prev_node.next + curr_node = self.head.next while curr_node is not None: if curr_node.data == data: prev_node.next = curr_node.next return - else: - prev_node = curr_node - curr_node = curr_node.next + prev_node = curr_node + curr_node = curr_node.next def delete_alt(self, data): if data is None: diff --git a/linked_lists/linked_list/linked_list_solution.ipynb b/linked_lists/linked_list/linked_list_solution.ipynb index fb25d2e..cebf074 100644 --- a/linked_lists/linked_list/linked_list_solution.ipynb +++ b/linked_lists/linked_list/linked_list_solution.ipynb @@ -231,8 +231,6 @@ " def find(self, data):\n", " if data is None:\n", " return None\n", - " if self.head is None:\n", - " return None\n", " curr_node = self.head\n", " while curr_node is not None:\n", " if curr_node.data == data:\n", @@ -245,15 +243,17 @@ " return\n", " if self.head is None:\n", " return\n", + " if self.head.data == data:\n", + " self.head = None\n", + " return\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", " if curr_node.data == data:\n", " prev_node.next = curr_node.next\n", " return\n", - " else:\n", - " prev_node = curr_node\n", - " curr_node = curr_node.next\n", + " prev_node = curr_node\n", + " curr_node = curr_node.next\n", "\n", " def delete_alt(self, data):\n", " if data is None:\n",