12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import pytest
- 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
- class ListNode:
- def __init__(self, val=0, next=None):
- self.val = val
- self.next = next
- def test_arr_to_linklist():
- arr = [1, 3, 4, 5, 6]
- head = arr_to_linklist(arr)
- arr1 = linkList_to_arr(head)
- assert arr == arr1
|