mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish linked list challenge and solution
Update algorithm discussion, solution, and constraints.
This commit is contained in:
parent
1e9ffd3f33
commit
58409ac286
|
@ -1,7 +1,7 @@
|
|||
class Node(object):
|
||||
|
||||
def __init__(self, data, next_node=None):
|
||||
self.next = next_node
|
||||
def __init__(self, data, next=None):
|
||||
self.next = next
|
||||
self.data = data
|
||||
|
||||
def __str__(self):
|
||||
|
@ -23,40 +23,35 @@ class LinkedList(object):
|
|||
|
||||
def insert_to_front(self, data):
|
||||
if data is None:
|
||||
return
|
||||
node = Node(data)
|
||||
if self.head is None:
|
||||
self.head = node
|
||||
else:
|
||||
node.next = self.head
|
||||
self.head = node
|
||||
return None
|
||||
node = Node(data, self.head)
|
||||
self.head = node
|
||||
return node
|
||||
|
||||
def append(self, data, next_node=None):
|
||||
def append(self, data):
|
||||
if data is None:
|
||||
return
|
||||
node = Node(data, next_node)
|
||||
return None
|
||||
node = Node(data)
|
||||
if self.head is None:
|
||||
self.head = node
|
||||
else:
|
||||
curr_node = self.head
|
||||
while curr_node.next is not None:
|
||||
curr_node = curr_node.next
|
||||
curr_node.next = node
|
||||
self.head = Node(data)
|
||||
return node
|
||||
curr_node = self.head
|
||||
while curr_node.next is not None:
|
||||
curr_node = curr_node.next
|
||||
curr_node.next = node
|
||||
return node
|
||||
|
||||
def find(self, data):
|
||||
if data is None:
|
||||
return
|
||||
return None
|
||||
if self.head is None:
|
||||
return
|
||||
return None
|
||||
curr_node = self.head
|
||||
while curr_node is not None:
|
||||
if curr_node.data == data:
|
||||
return curr_node
|
||||
else:
|
||||
curr_node = curr_node.next
|
||||
return
|
||||
curr_node = curr_node.next
|
||||
return None
|
||||
|
||||
def delete(self, data):
|
||||
if data is None:
|
||||
|
@ -73,6 +68,21 @@ class LinkedList(object):
|
|||
prev_node = curr_node
|
||||
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):
|
||||
curr_node = self.head
|
||||
while curr_node is not None:
|
||||
|
|
|
@ -39,7 +39,9 @@
|
|||
"* Is this a circular list?\n",
|
||||
" * No\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",
|
||||
" # TODO: Implement me\n",
|
||||
"\n",
|
||||
" def append(self, data, next_node=None):\n",
|
||||
" def append(self, data):\n",
|
||||
" pass\n",
|
||||
" # TODO: Implement me\n",
|
||||
"\n",
|
||||
|
@ -310,21 +312,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -38,7 +38,9 @@
|
|||
"* Is this a circular list?\n",
|
||||
" * No\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",
|
||||
"\n",
|
||||
"* If the data we are inserting is None, return\n",
|
||||
"* Create a node with the input data\n",
|
||||
"* If this is an empty list\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",
|
||||
"* Create a node with the input data, set node.next to head\n",
|
||||
"* Assign the head to the node\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
|
@ -138,6 +136,9 @@
|
|||
" * 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",
|
||||
" * 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",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
|
@ -188,8 +189,8 @@
|
|||
"%%writefile linked_list.py\n",
|
||||
"class Node(object):\n",
|
||||
"\n",
|
||||
" def __init__(self, data, next_node=None):\n",
|
||||
" self.next = next_node\n",
|
||||
" def __init__(self, data, next=None):\n",
|
||||
" self.next = next\n",
|
||||
" self.data = data\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
|
@ -211,40 +212,35 @@
|
|||
"\n",
|
||||
" def insert_to_front(self, data):\n",
|
||||
" if data is None:\n",
|
||||
" return\n",
|
||||
" node = Node(data)\n",
|
||||
" if self.head is None:\n",
|
||||
" self.head = node\n",
|
||||
" else:\n",
|
||||
" node.next = self.head\n",
|
||||
" self.head = node\n",
|
||||
" return None\n",
|
||||
" node = Node(data, self.head)\n",
|
||||
" self.head = node\n",
|
||||
" return node\n",
|
||||
"\n",
|
||||
" def append(self, data, next_node=None):\n",
|
||||
" def append(self, data):\n",
|
||||
" if data is None:\n",
|
||||
" return\n",
|
||||
" node = Node(data, next_node)\n",
|
||||
" return None\n",
|
||||
" node = Node(data)\n",
|
||||
" if self.head is None:\n",
|
||||
" self.head = node\n",
|
||||
" else:\n",
|
||||
" curr_node = self.head\n",
|
||||
" while curr_node.next is not None:\n",
|
||||
" curr_node = curr_node.next\n",
|
||||
" curr_node.next = node\n",
|
||||
" self.head = Node(data)\n",
|
||||
" return node\n",
|
||||
" curr_node = self.head\n",
|
||||
" while curr_node.next is not None:\n",
|
||||
" curr_node = curr_node.next\n",
|
||||
" curr_node.next = node\n",
|
||||
" return node\n",
|
||||
"\n",
|
||||
" def find(self, data):\n",
|
||||
" if data is None:\n",
|
||||
" return\n",
|
||||
" return None\n",
|
||||
" if self.head is None:\n",
|
||||
" return\n",
|
||||
" return None\n",
|
||||
" curr_node = self.head\n",
|
||||
" while curr_node is not None:\n",
|
||||
" if curr_node.data == data:\n",
|
||||
" return curr_node\n",
|
||||
" else:\n",
|
||||
" curr_node = curr_node.next\n",
|
||||
" return\n",
|
||||
" curr_node = curr_node.next\n",
|
||||
" return None\n",
|
||||
"\n",
|
||||
" def delete(self, data):\n",
|
||||
" if data is None:\n",
|
||||
|
@ -261,6 +257,21 @@
|
|||
" prev_node = curr_node\n",
|
||||
" curr_node = curr_node.next\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",
|
||||
" curr_node = self.head\n",
|
||||
" while curr_node is not None:\n",
|
||||
|
@ -477,21 +488,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
Loading…
Reference in New Issue
Block a user