Polish linked list challenge and solution

Update algorithm discussion, solution, and constraints.
This commit is contained in:
Donne Martin 2016-06-12 23:29:58 -04:00
parent 1e9ffd3f33
commit 58409ac286
3 changed files with 88 additions and 65 deletions

View File

@ -1,7 +1,7 @@
class Node(object): class Node(object):
def __init__(self, data, next_node=None): def __init__(self, data, next=None):
self.next = next_node self.next = next
self.data = data self.data = data
def __str__(self): def __str__(self):
@ -23,40 +23,35 @@ class LinkedList(object):
def insert_to_front(self, data): def insert_to_front(self, data):
if data is None: if data is None:
return return None
node = Node(data) node = Node(data, self.head)
if self.head is None: self.head = node
self.head = node
else:
node.next = self.head
self.head = node
return node return node
def append(self, data, next_node=None): def append(self, data):
if data is None: if data is None:
return return None
node = Node(data, next_node) node = Node(data)
if self.head is None: if self.head is None:
self.head = node self.head = Node(data)
else: return node
curr_node = self.head curr_node = self.head
while curr_node.next is not None: while curr_node.next is not None:
curr_node = curr_node.next curr_node = curr_node.next
curr_node.next = node curr_node.next = node
return node return node
def find(self, data): def find(self, data):
if data is None: if data is None:
return return None
if self.head is None: if self.head is None:
return 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:
return curr_node return curr_node
else: curr_node = curr_node.next
curr_node = curr_node.next return None
return
def delete(self, data): def delete(self, data):
if data is None: if data is None:
@ -73,6 +68,21 @@ class LinkedList(object):
prev_node = curr_node prev_node = curr_node
curr_node = curr_node.next curr_node = curr_node.next
def delete_alt(self, data):
if data is None:
return
if self.head is None:
return
curr_node = self.head
if curr_node.data == data:
curr_node = curr_node.next
return
while curr_node.next is not None:
if curr_node.next.data == data:
curr_node.next = curr_node.next.next
return
curr_node = curr_node.next
def print_list(self): def print_list(self):
curr_node = self.head curr_node = self.head
while curr_node is not None: while curr_node is not None:

View File

@ -39,7 +39,9 @@
"* Is this a circular list?\n", "* Is this a circular list?\n",
" * No\n", " * No\n",
"* Do we keep track of the tail or just the head?\n", "* Do we keep track of the tail or just the head?\n",
" * Just the head" " * Just the head\n",
"* Can we insert None values?\n",
" * No"
] ]
}, },
{ {
@ -133,7 +135,7 @@
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
"\n", "\n",
" def append(self, data, next_node=None):\n", " def append(self, data):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
"\n", "\n",
@ -310,21 +312,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -38,7 +38,9 @@
"* Is this a circular list?\n", "* Is this a circular list?\n",
" * No\n", " * No\n",
"* Do we keep track of the tail or just the head?\n", "* Do we keep track of the tail or just the head?\n",
" * Just the head" " * Just the head\n",
"* Can we insert None values?\n",
" * No"
] ]
}, },
{ {
@ -92,12 +94,8 @@
"### Insert to Front\n", "### Insert to Front\n",
"\n", "\n",
"* If the data we are inserting is None, return\n", "* If the data we are inserting is None, return\n",
"* Create a node with the input data\n", "* Create a node with the input data, set node.next to head\n",
"* If this is an empty list\n", "* Assign the head to the node\n",
" * Assign the head to the node\n",
"* Else\n",
" * Set the node.next to the head\n",
" * Set the head to the node\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1)\n", "* Time: O(1)\n",
@ -138,6 +136,9 @@
" * Update previous node's next pointer to the current node's next pointer\n", " * Update previous node's next pointer to the current node's next pointer\n",
" * We do not have have to explicitly delete in Python\n", " * We do not have have to explicitly delete in Python\n",
" * Else, move on to the next node\n", " * Else, move on to the next node\n",
"* As an alternative, we could avoid the use of two pointers by evaluating the current node's next value:\n",
" * If the next value is a match, set the current node's next to next.next\n",
" * Special care should be taken if deleting the head node\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
@ -188,8 +189,8 @@
"%%writefile linked_list.py\n", "%%writefile linked_list.py\n",
"class Node(object):\n", "class Node(object):\n",
"\n", "\n",
" def __init__(self, data, next_node=None):\n", " def __init__(self, data, next=None):\n",
" self.next = next_node\n", " self.next = next\n",
" self.data = data\n", " self.data = data\n",
"\n", "\n",
" def __str__(self):\n", " def __str__(self):\n",
@ -211,40 +212,35 @@
"\n", "\n",
" def insert_to_front(self, data):\n", " def insert_to_front(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return None\n",
" node = Node(data)\n", " node = Node(data, self.head)\n",
" if self.head is None:\n", " self.head = node\n",
" self.head = node\n",
" else:\n",
" node.next = self.head\n",
" self.head = node\n",
" return node\n", " return node\n",
"\n", "\n",
" def append(self, data, next_node=None):\n", " def append(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return None\n",
" node = Node(data, next_node)\n", " node = Node(data)\n",
" if self.head is None:\n", " if self.head is None:\n",
" self.head = node\n", " self.head = Node(data)\n",
" else:\n", " return node\n",
" curr_node = self.head\n", " curr_node = self.head\n",
" while curr_node.next is not None:\n", " while curr_node.next is not None:\n",
" curr_node = curr_node.next\n", " curr_node = curr_node.next\n",
" curr_node.next = node\n", " curr_node.next = node\n",
" return node\n", " return node\n",
"\n", "\n",
" def find(self, data):\n", " def find(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return None\n",
" if self.head is None:\n", " if self.head is None:\n",
" return\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",
" return curr_node\n", " return curr_node\n",
" else:\n", " curr_node = curr_node.next\n",
" curr_node = curr_node.next\n", " return None\n",
" return\n",
"\n", "\n",
" def delete(self, data):\n", " def delete(self, data):\n",
" if data is None:\n", " if data is None:\n",
@ -261,6 +257,21 @@
" 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",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" curr_node = self.head\n",
" if curr_node.data == data:\n",
" curr_node = curr_node.next\n",
" return\n",
" while curr_node.next is not None:\n",
" if curr_node.next.data == data:\n",
" curr_node.next = curr_node.next.next\n",
" return\n",
" curr_node = curr_node.next\n",
"\n",
" def print_list(self):\n", " def print_list(self):\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",
@ -477,21 +488,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,