-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.py
More file actions
39 lines (32 loc) · 1.05 KB
/
ListNode.py
File metadata and controls
39 lines (32 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
# File : ListNode.py
# Author : MeteorDream
# Time : 2021/06/04 17:16:30
# language: Python
# Software: Visual Studio Code
from typing import Iterable
class ListNode:
""" You can create LinkList like ListNode.CreateList(range(10), False) """
def __init__(self, x=0, next=None):
self.val = x
self.next = next
def __str__(self):
if self.next is not None:
return f"{self.val} -> {str(self.next)}"
return str(self.val)
def __format__(self, __format_spec: str) -> str:
return self.val.__format__(__format_spec)
def __repr__(self):
return self.__str__()
@staticmethod
def CreateList(nums: Iterable, head: bool=True):
""" Create LinkList according to nums with head node """
dummy = ListNode()
pre = dummy
for v in nums:
pre.next = ListNode(v)
pre = pre.next
return dummy if head else dummy.next
if __name__ == '__main__':
root = ListNode.CreateList(range(10), False)
print(root)