123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import pytest
- # 链表
- class ListNode:
- def __init__(self, val=0, next=None):
- self.val = val
- self.next = next
- # 数组转链表
- def arr_to_linklist(arr):
- p = None
- for n in reversed(arr):
- node = ListNode(n)
- node.next = p
- p = node
- return p
- # 链表转数组
- def linkList_to_arr(head):
- res = []
- cur = head
- while cur is not None:
- res.append(cur.val)
- cur = cur.next
- return res
- # 数组转链表
- def list_to_linklist(arr):
- head = ListNode(arr[0])
- p = head
- for i in range(1, len(arr)):
- p.next = ListNode(arr[i])
- p = p.next
- return head
- # 计算链表长度
- def len_of_linklist(head):
- lens = 0
- p = head
- while p:
- lens += 1
- p = p.next
- return lens
- def deleteNode(node):
- node.val = node.next.val
- node.next = node.next.next
|