-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertNodeAtPosition.py
More file actions
181 lines (117 loc) · 3.71 KB
/
Copy pathinsertNodeAtPosition.py
File metadata and controls
181 lines (117 loc) · 3.71 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
175
176
177
178
179
180
"""
You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node.
A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.
As an example, if your list starts as
and you want to insert a node at position with , your new list should be
Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.
insertNodeAtPosition has the following parameters:
head: a SinglyLinkedListNode pointer to the head of the list
data: an integer value to insert as data in your new node
position: an integer position to insert the new node, zero based indexing
Input Format
The first line contains an integer
, the number of elements in the linked list.
Each of the next lines contains an integer SinglyLinkedListNode[i].data.
The last line contains an integer
.
Constraints
, where is the
element of the linked list.
.
Output Format
Return a reference to the list head. Locked code prints the list for you.
Sample Input
3
16
13
7
1
2
Sample Output
16 13 1 7
Explanation
The initial linked list is 16 13 7. We have to insert
at the position which currently has in it. The updated linked list will be 16 13 1 7
"""
#!/bin/python3
import math
import os
import random
import re
import sys
class SinglyLinkedListNode:
def __init__(self, node_data):
self.data = node_data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def insert_node(self, node_data):
node = SinglyLinkedListNode(node_data)
if not self.head:
self.head = node
else:
self.tail.next = node
self.tail = node
def print_singly_linked_list(node, sep, fptr):
while node:
fptr.write(str(node.data))
node = node.next
if node:
fptr.write(sep)
# Complete the insertNodeAtPosition function below.
#
# For your reference:
#
# SinglyLinkedListNode:
# int data
# SinglyLinkedListNode next
#
#
def insertNodeAtPosition(head, data, position):
if head == None:
head = SinglyLinkedListNode(data)
else:
temp = head
last = SinglyLinkedListNode(data)
if position == 0:
last.next = temp
head = last
else:
count = 1
while count != position:
temp = temp.next
count += 1
last.next = temp.next
temp.next = last
return head
return head
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
# llist_count = int(input())
# llist = SinglyLinkedList()
# for _ in range(llist_count):
# llist_item = int(input())
# llist.insert_node(llist_item)
# data = int(input())
# position = int(input())
# llist_head = insertNodeAtPosition(llist.head, data, position)
# print_singly_linked_list(llist_head, ' ', fptr)
# fptr.write('\n')
# fptr.close()
llist_count = 3
llist = SinglyLinkedList()
array = [16, 13, 7]
for _ in range(llist_count):
llist_item = array[_]
print(llist_item)
llist.insert_node(llist_item)
data = 1
position = 2
llist_head = insertNodeAtPosition(llist.head, data, position)
result = []
while llist_head:
result.append(llist_head.data)
llist_head = llist_head.next
print(result)