# python创建链表

class SingleNode(object):
"""单链表的结点"""

def __init__(self, val):
# val存放数据元素
self.val = val
# _next是下一个节点的标识
self.next = None

"""单链表"""

def __init__(self):
self.length = 0

def is_empty(self):
"""判断链表是否为空"""
return self._head == None

# def length(self):
#     """链表长度"""
#     # cur初始时指向头节点
#     cur = self._head
#     count = 0
#     # 尾节点指向None，当未到达尾部时
#     while cur != None:
#         count += 1
#         # 将cur后移一个节点
#         cur = cur.next
#     return count

def get(self, index):
if index < 0 or index >= self.length:
return -1
else:

cnt = 0
while cur:
if index == cnt:
return cur.val
else:
cnt += 1
cur = cur.next

"""头部添加元素"""
# 先创建一个保存val值的节点
node = SingleNode(val)
self.length += 1

"""尾部添加元素"""
node = SingleNode(val)
if self.is_empty():
# 若不为空，则找到尾部，将尾节点的next指向新节点
else:
while cur.next:
cur = cur.next
cur.next = node
self.length += 1

def addAtIndex(self, index, val):
"""指定位置添加元素"""
# 若指定位置index为第一个元素之前，则执行头部插入
if index <= 0:
# 若指定位置超过链表尾部，则执行尾部插入
elif index > (self.length - 1):
# 找到指定位置
else:
node = SingleNode(val)
count = 0
# pre用来指向指定位置pos的前一个位置pos-1，初始从头节点开始移动到指定位置
while count < (index - 1):
count += 1
pre = pre.next
# 先将新节点node的next指向插入位置的节点
node.next = pre.next
# 将插入位置的前一个节点的next指向新节点
pre.next = node
self.length += 1

def deleteAtIndex(self, index):
"""
Delete the index-th node in the linked list, if the index is valid.
:type index: int
:rtype: void
"""
node = SingleNode(0)
if index <= 0 or index >= self.length:
return -1
else:
pre = None
cnt = 0
while cnt < index:
pre = cur
cnt += 1
cur = cur.next

pre.next = cur.next
self.length -= 1

def search(self, item):
"""链表查找节点是否存在，并返回True或者False"""
while cur != None:
if cur.item == item:
return True
cur = cur.next
return False

def travel(self):
"""遍历链表"""
while cur != None:
print (cur.val)
cur = cur.next
print("")

#
obj.travel()
obj.travel()
obj.travel()
print(obj.get(1))
print(obj.get(0))
print(obj.get(2))
# [[],[1],[1,2],[1],[0],[2]]
# 输出：[null,null,null,2,-1,-1]
# 预期：[null,null,null,2,1,-1]

# print('1...')
# print(li.get(0))
# print('2...')
# li.travel()
# print('3...')
# print(li.get(0))
# print('4...')
# print(li.get(1))
# 预期：[null,-1,null,-1,-1,null,1,-1]