-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07_singlyLL_nth_to_last.py
102 lines (76 loc) · 2.11 KB
/
07_singlyLL_nth_to_last.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
cur_node = self.head
self.head = new_node
new_node.next = cur_node
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def len_of_list(self):
count = 0
cur_node = self.head
while cur_node:
count += 1
cur_node = cur_node.next
return count
def print_nth_from_last_method1(self, n):
# method-1:
total_len = self.len_of_list()
cur = self.head
while cur:
if total_len == n:
print(cur.data)
return cur
total_len -= 1
cur = cur.next
if cur is None:
return "No node present"
def print_nth_from_last_method2(self, n):
# method-2
p = self.head
q = self.head
count = 0
while q and count < n:
q = q.next
count += 1
if not q and count < n:
print(str(n) + " is greater than the number of nodes in list. ")
return
while p and q:
p = p.next
q = q.next
return p.data
if __name__ == "__main__":
llist = SinglyLinkedList()
llist.append("A")
llist.append("B")
llist.append("C")
llist.append("D")
llist.append("E")
llist.append("F")
llist.print_list()
print("\n")
print(llist.print_nth_from_last_method1(3))
print("\n")
print(llist.print_nth_from_last_method2(3))