单链表的增删改查
class ListNode(object):
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def BuildListNode(l: list):
head = ListNode()
temp = head
for elem in l:
temp.next = ListNode(elem)
temp = temp.next
return head
def BuildListNodeReverse(l: list):
head = ListNode()
for elem in l:
temp = head.next
head.next = ListNode(elem, temp)
return head
def PrintListNode(head: ListNode):
cur = head
print('_________')
while cur.next:
cur = cur.next
print(cur.value)
def Insert(head: ListNode, i, elem):
assert i >= 0
elem = ListNode(elem)
cur = head
while i != 0:
i -= 1
cur = cur.next
if not cur:
return False
temp = cur.next
cur.next = elem
elem.next = temp
return True
def Find(head: ListNode, i):
assert i >= 0
cur = head.next
while i != 0:
cur = cur.next
i -= 1
if not cur:
return False
return cur.value
def Delete(head: ListNode, i):
assert i >= 0
cur = head
while i != 0:
cur = cur.next
i -= 1
if not cur:
return False
cur.next = cur.next.next
return True