-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path100-singly_linked_list.py
More file actions
executable file
·72 lines (57 loc) · 1.74 KB
/
100-singly_linked_list.py
File metadata and controls
executable file
·72 lines (57 loc) · 1.74 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
"""Singley Linked List"""""
class Node:
"""Node class"""
def __init__(self, data, next_node=None):
"""Defines a node of LL"""
self.data = data
self.next_node = next_node
@property
def data(self):
"""data Getter"""
return self.__data
@data.setter
def data(self, value):
"""data Setter"""
if not isinstance(value, int):
raise TypeError('data must be an integer')
self.__data = value
@property
def next_node(self):
"""next_node Getter"""
return self.__next_node
@next_node.setter
def next_node(self, value):
"""next_node Setter"""
if value is not None and type(value) != Node:
raise TypeError('next_node must be a Node object')
self.__next_node = value
class SinglyLinkedList:
"""Singley Linked List class"""
def __init__(self):
"""Initialize SSL class"""
self.head = None
def __str__(self):
"""To string method"""
result = ""
node = self.head
while node:
result += str(node.data) + '\n'
node = node.next_node
return result[:-1]
def sorted_insert(self, value):
"""Inserts a new node at sorted position"""
new_node = Node(value)
if not self.head:
self.head = new_node
return
if value < self.head.data:
new_node.next_node = self.head
self.head = new_node
return
node = self.head
while node.next_node and node.next_node.data < value:
node = node.next_node
if node.next_node:
new_node.next_node = node.next_node
node.next_node = new_node