-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked_list_practice.py
More file actions
174 lines (144 loc) · 3.06 KB
/
linked_list_practice.py
File metadata and controls
174 lines (144 loc) · 3.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#https://leetcode.com/problems/intersection-of-two-linked-lists/submissions/
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def first_node(self):
if self.head:
cur_node = self.head
return cur_node.data
else:
return 'empty LinkedList'
def listed(self):
myList = []
head = self.head
while head:
myList.append(head.data)
head = head.next
return myList
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data, end=' ')
cur_node = cur_node.next
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def reverse(self):
head = self.head
prev = None
while head:
next_node = head.next
head.next = prev
prev = head
head = next_node
self.head = prev
# print('cur_node: {}\nnext_node: {}\nprev: {}\n'.format(cur_node.data, next_node.data, prev.data))
# return
def node_of_index(self, index):
head = self.head
counter = 0
while head:
if counter == index:
return head.data
head = head.next
counter+=1
def length(self):
head = self.head
counter = 0
while head.next:
head = head.next
counter += 1
return counter
def insert_node(self, data, index):
head = self.head
if index == 0:
self.prepend(data)
if index > self.length():
self.append(data)
return
new_node = Node(data)
counter = 1
while head:
if counter == index:
pre = head.next
head.next = new_node
new_node.next = pre
head = head.next
counter+=1
def remove_index(self, index):
head = self.head
if index >= self.length():
return "index out of bound"
if index == 0:
self.head = head.next
return
head = self.head
counter = 0
prev = None
while head:
if counter == index:
prev.next = head.next
return
prev = head
head = head.next
def intersect(self, headA, headB):
myList = []
while headA:
myList.append(headA.val)
headA = headA.next
while headB:
if headB.val in myList:
return headB
headB = headB.next
return None
# a = Node('a')
# b = Node('b')
# c = Node('c')
llist = LinkedList()
# llist.head = a
# llist.head.next = b
# b.next = c
# # print(llist.head.data)
# # print(a.next.next.data)
# while llist.head:
# print(llist.head.data)
# llist.head = llist.head.next
# print(llist.first_node())
llist.append('C')
llist.append('D')
llist.append('E')
llist.prepend('B')
llist.prepend('A')
llist.print_list()
print()
print('after reverse')
llist.reverse()
llist.print_list()
j = llist.node_of_index(4)
print('\n',j)
llist.insert_node('hi',5)
print(llist.length())
print()
llist.print_list()
llist.remove_index(0)
print()
llist.print_list()
print()
bList = LinkedList()
bList.append(1)
bList.append(llist.node_of_index(2))
bList.print_list()