-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNthNode.py
138 lines (129 loc) · 2.82 KB
/
NthNode.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
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
# Given a linked list, remove the n-th node from the end of list and return its head.
# Given linked list: 1->2->3->4->5, and n = 2.
# After removing the second node from the end, the linked list becomes 1->2->3->5.
# Given n will always be valid
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class linkList(object):
def __init__(self):
self.head = None
def add2Last(self, data):
newNode = Node(data)
if self.head == None:
self.head = newNode
else:
temp = self.head
while(temp.next):
temp = temp.next
temp.next = newNode
newNode.next = None
def traverse(self):
if self.head != None:
temp = self.head
while (temp):
print(temp.data, end = "->")
temp = temp.next
else:
return
def countNodes(self):
if self.head == None:
return 0
else:
count = 0
temp = self.head
while(temp.next):
count +=1
temp = temp.next
return count+1
def nthNodefromLast(self, n):
"""
Implementation 1
"""
c = self.countNodes()
ctr = 0
nodeatPos = c-n
temp = self.head
while(temp):
temp =temp.next
ctr +=1
if ctr == nodeatPos:
break
return temp.data
def nthNode1pass(self, n):
'''
1 10 12 15 45 50
^
|
'''
pt1 = pt2 = self.head
c = 0
while(pt1.next):
pt1 = pt1.next
c = c+1
if c==(n-1):
break
while pt1.next:
prev = pt2
pt2 = pt2.next
pt1 = pt1.next
return pt2.data
def removeNthfromLast(self, n):
dummy = Node(-1)
dummy.next = self.head
self.head = dummy
pt1 = pt2 = self.head
c = 1
while(pt1.next):
pt1 = pt1.next
c = c+1
if c==(n):
print("c " , c)
break
while pt1.next:
prev = pt2
pt2 = pt2.next
pt1 = pt1.next
prev.next = pt2.next
del pt2
# return dummy.next
def removeNth(self, n):
dummy = Node(-1)
dummy.next = self.head
self.head = dummy
c = self.countNodes()
ctr = 0
nodeatPos = c-n
temp = self.head
while(temp):
prev = temp
temp = temp.next
ctr +=1
if ctr == nodeatPos:
break
prev.next = temp.next
del temp
return dummy.next
# *** LINKLIST ***
ll = linkList()
ll.head = Node(1)
ll.head.next = Node(10)
ll.add2Last(12)
ll.add2Last(15)
ll.add2Last(45)
ll.add2Last(50)
ll.traverse()
print("\n Nth node ...(1) using count and (2) using single pass")
print(ll.nthNodefromLast(2))
print(ll.nthNode1pass(2))
print("\n Nth node from last ...")
ll.removeNth(2)
ll.traverse()
print("\n special case ...")
ll2 = linkList()
ll2.head = Node(1)
ll2.head.next = None
ll2.traverse()
print("\n removing the single node")
print(ll2.removeNth(1))