Example:class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
def get_position(self, position):
"""Get an element from a particular position.
Assume the first position is "1".
Return "None" if position is not in the list."""
current = self.head
num_element = 1
if position < 1:
return None
while current is not None:
if num_element == position:
return current
current = current.next
num_element += 1
return None
def insert(self, new_element, position):
"""Insert a new node at the given position.
Assume the first position is "1".
Inserting at position 3 means between
the 2nd and 3rd elements."""
current = self.head
if position > 1:
num_element = 1
while num_element < position - 1 and current.next:
current = current.next
num_element += 1
new_element.next = current.next
current.next = new_element
else:
new_element.next = current
self.head = new_element
def delete(self, value):
"""Delete the first node with a given value."""
current = self.head
previous = None
while current.value != value and current.next:
previous = current
current = current.next
if current.value == value:
if previous:
previous.next = current.next
else:
self.head = current.next