Skip to content

Commit 4185975

Browse files
finished 2.3
1 parent 0ffd517 commit 4185975

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
Delete Middle Node: Implement an algorithm to delete a node in the middle
3+
(i.e., any node but the first and last node, not necessarily the exact middle)
4+
of a singly linked list, given only access to that node.
5+
EXAMPLE
6+
lnput:the node c from the linked list a->b->c->d->e->f
7+
Result: nothing is returned, but the new linked list looks like a->b->d->e- >f
8+
Hints:#72
9+
"""
10+
class Node:
11+
#Singly link list
12+
def __init__(self,data):
13+
self.data = data
14+
self.next = None
15+
16+
class linklist:
17+
#linkList class
18+
def __init__(self):
19+
self.head = None
20+
self.size = 0
21+
22+
def push(self,data):
23+
node = Node(data)# create a new node
24+
25+
if self.head == None: #check to see if the head node is empty
26+
self.head = node # If its empty add it to the new node
27+
self.size = 1
28+
return
29+
#if the head of the linklist is filled
30+
31+
current = self.head
32+
while current.next is not None:#Check the current postion is empty
33+
#Move to the next line if nothing is there
34+
current = current.next
35+
36+
37+
current.next = node #point self.head to a new node
38+
self.size+=1
39+
40+
41+
42+
43+
def lenght(self):
44+
#note the count doesn't start at zero
45+
cur = self.head
46+
counter = 0
47+
while cur is not None:
48+
counter+=1
49+
cur = cur.next
50+
print('Linklist len: '+str(counter))
51+
return counter
52+
53+
def printList(self):
54+
curr = self.head
55+
elem = []
56+
57+
while(curr != None):
58+
elem.append(curr.data)
59+
curr = curr.next
60+
print(elem)
61+
#1->2->3
62+
def remove_node(self,data):
63+
#1->2->3
64+
curr = self.head
65+
if curr is not None and curr.data == data:
66+
self.head = curr.next
67+
curr = None
68+
#look for the node that has the data we are looking for
69+
while curr is not None:
70+
if curr.data == data:
71+
break
72+
prev = curr
73+
curr = curr.next
74+
75+
#if data isn't found just reutrn
76+
if(curr == None):
77+
return
78+
79+
#allow us to unlink the nodes
80+
prev.next = curr.next
81+
curr = None
82+
"""
83+
def Kth_to_Last(self,num):
84+
counter = 1 # We need to keep track where we are
85+
curr = self.head
86+
goal = self.size - num# kth to last element
87+
while curr is not None:
88+
if goal == counter:
89+
return curr.data
90+
counter+=1
91+
curr = curr.next
92+
"""
93+
def Delete_Middle_Node(self):
94+
counter = 1
95+
curr = self.head
96+
goal = (self.size//2)# floor division
97+
while curr is not None:
98+
if counter == goal:
99+
self.remove_node(curr.data)
100+
return
101+
counter+=1
102+
curr = curr.next
103+
104+
105+
106+
107+
108+
109+
llist = linklist()
110+
llist.push(1)
111+
llist.push(2)
112+
llist.push(3)
113+
llist.push(4)
114+
llist.push(5)
115+
llist.push(6)
116+
llist.printList()
117+
llist.lenght()
118+
119+
llist.Delete_Middle_Node()# 3 should be removed
120+
llist.printList()
121+
llist.lenght()

0 commit comments

Comments
 (0)