-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathInsert_node.py
More file actions
59 lines (42 loc) · 1023 Bytes
/
Insert_node.py
File metadata and controls
59 lines (42 loc) · 1023 Bytes
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
class Node:
def __init__(Node, data):
Node.data = data
Node.next = None
def insertNode(head, newNode):
newNode.next = head
head = newNode
return head
def insertmiddle(head, newNode):
curr = head
while (curr.data != 'e'):
curr = curr.next
newNode.next = curr.next
curr.next = newNode
return head
def insertend(head, newNode):
curr = head
while (curr.next != None):
curr = curr.next
curr.next = newNode
newNode.next = None
return head
def printList(head):
temp = head
while (temp):
print(temp.data)
temp = temp.next
head = Node('a')
nodeB = Node('b')
nodeC = Node('c')
nodeD = Node('d')
nodeE = Node('e')
nodeF = Node('f')
head.next = nodeB
nodeB.next = nodeC
nodeC.next = nodeD
nodeD.next = nodeE
nodeE.next = nodeF
# head=insertNode(head,Node('z'))
# head=insertmiddle(head,Node('z'))
head = insertend(head, Node('z'))
printList(head)