forked from sat5297/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linkedlist.py
49 lines (48 loc) · 1.06 KB
/
Linkedlist.py
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
class Node:
def __init__(self, data = None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
newNode = Node(data)
if(self.head):
current = self.head
while(current.next):
current = current.next
current.next = newNode
else:
self.head = newNode
def printLL(self):
current = self.head
while(current):
print(current.data)
current = current.next
def removeNthFromEnd(self, n):
temp = self.head
k=0
while temp!=None:
k+=1
temp=temp.next
pos=k-n
curr_pos=0
prev=None
temp=self.head
while curr_pos!=pos:
prev=temp
temp=temp.next
curr_pos+=1
if prev:
prev.next=temp.next
else:
self.head=self.head.next
return self.head
keys = list(map(int, input().split(" ")))
LL = LinkedList()
for key in keys:
LL.insert(key)
#LL.printLL()
x=int(input())
LL.removeNthFromEnd(x)
LL.printLL()