-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListUtil.py
32 lines (25 loc) · 843 Bytes
/
LinkedListUtil.py
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
from typing import List
class ListNode:
"""Definition for singly-linked list."""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class LinkedLists:
"""Collection of linked list utilities"""
@staticmethod
def create_linked_list(arr: List) -> ListNode:
"""Method to create a linked list from a List"""
head = ListNode('head')
next_head = head
for element in arr:
next_head.next = ListNode(element)
next_head = next_head.next
return head.next
@staticmethod
def print_linked_list(head: ListNode) -> None:
"""Method to print the contents of a linked list"""
output = []
while head:
output.append(str(head.val))
head = head.next
print(' -> '.join(output))