Skip to content

Commit 7f615a8

Browse files
working on 2.4
1 parent 4185975 commit 7f615a8

File tree

2 files changed

+130
-11
lines changed

2 files changed

+130
-11
lines changed

Python/chapter02/2.3 - Delete Middle Node/camilo_solution_2.3.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,7 @@ def remove_node(self,data):
7979
#allow us to unlink the nodes
8080
prev.next = curr.next
8181
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-
"""
82+
9383
def Delete_Middle_Node(self):
9484
counter = 1
9585
curr = self.head
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""
2+
Partition: Write code to partition a linked list around a value x, such that all
3+
nodes less than x come before all nodes greater than or equal to x. If x is
4+
contained within the list, the values of x only need to be after the elements
5+
less than x (see below). The partition element x can appear anywhere in the
6+
"right partition"; it does not need to appear between the left and right
7+
partitions.
8+
EXAMPLE
9+
Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1[partition=5] Output: 3 -> 1 -> 2 ->
10+
10 -> 5 -> 5 -> 8
11+
Hints: #3, #24
12+
"""
13+
14+
class Node:
15+
#Singly link list
16+
def __init__(self,data = None):
17+
self.data = data
18+
self.next = None
19+
20+
class linklist:
21+
#linkList class
22+
def __init__(self):
23+
self.head = None
24+
self.size = 0
25+
26+
def push(self,data):
27+
node = Node(data)# create a new node
28+
29+
if self.head == None: #check to see if the head node is empty
30+
self.head = node # If its empty add it to the new node
31+
self.size = 1
32+
return
33+
#if the head of the linklist is filled
34+
35+
current = self.head
36+
while current.next is not None:#Check the current postion is empty
37+
#Move to the next line if nothing is there
38+
current = current.next
39+
40+
41+
current.next = node #point self.head to a new node
42+
self.size+=1
43+
44+
def lenght(self):
45+
#note the count doesn't start at zero
46+
cur = self.head
47+
counter = 0
48+
while cur is not None:
49+
counter+=1
50+
cur = cur.next
51+
print('Linklist len: '+str(counter))
52+
return counter
53+
54+
def printList(self):
55+
curr = self.head
56+
elem = []
57+
58+
while(curr != None):
59+
elem.append(curr.data)
60+
curr = curr.next
61+
print(elem)
62+
#1->2->3
63+
def remove_node(self,data):
64+
#1->2->3
65+
curr = self.head
66+
if curr is not None and curr.data == data:
67+
self.head = curr.next
68+
curr = None
69+
#look for the node that has the data we are looking for
70+
while curr is not None:
71+
if curr.data == data:
72+
break
73+
prev = curr
74+
curr = curr.next
75+
76+
#if data isn't found just reutrn
77+
if(curr == None):
78+
return
79+
80+
#allow us to unlink the nodes
81+
prev.next = curr.next
82+
curr = None
83+
84+
def merge_link(self,NodesA,NodesB):
85+
86+
while NodesA is not None:
87+
Nodec = Node()
88+
89+
90+
def partition(self,num):
91+
92+
curr= self.head
93+
while curr is not None:
94+
if(curr.data > num):
95+
smallNodes = self.push(curr.data)
96+
curr = curr.next
97+
else:
98+
lagreNodes = self.(curr.data)
99+
curr = curr.next
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
llist = linklist()
118+
llist.push(1)
119+
llist.push(2)
120+
llist.push(3)
121+
llist.push(4)
122+
llist.push(5)
123+
llist.push(6)
124+
llist.printList()
125+
llist.lenght()
126+
127+
llist.Delete_Middle_Node()# 3 should be removed
128+
llist.printList()
129+
llist.lenght()

0 commit comments

Comments
 (0)