一、链表(Linked List)
访问 Access O(N)
搜索 Search O(N)
插入 Insert O(1)
删除 Delete O(1)
链表写很快,读很慢,适合于读少写多的场景。
二、Python 链表常用操作
1、创建链表
2、添加元素
3、访问元素
4、查找元素
5、更新元素
6、删除元素
7、链表的长度
三、力扣203题
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
prev = dummy
while(head is not None):
if head.val == val:
prev.next = head.next
head = head.next
else:
prev = head
head = head.next
return dummy.next
四、力扣206题
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
while head is not None and head.next is not None:
dnext = dummy.next
hnext = head.next
dummy.next = hnext
head.next = hnext.next
hnext.next = dnext
return dummy.next