-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo_6_Alternative_Elements.py
More file actions
88 lines (80 loc) · 2.46 KB
/
Video_6_Alternative_Elements.py
File metadata and controls
88 lines (80 loc) · 2.46 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
#Find Sum, Count, Average of a Linked List
class Node:
def __init__(self,data):
self.info=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def insbeg(self,ele):
current=Node(ele)
if self.head is None:
self.head=current
else:
current.next=self.head
self.head=current
def display(self):
if self.head is None:
print("Empty Linked List")
return
else:
current=self.head
while current:
print(current.info," -> ", end="")
current=current.next
print("None")
#Approach 1 using count variable
def displayAlternate1(self):
if self.head is None:
print("Empty Linked List")
return
else:
count=1
current=self.head
while current:
if count%2!=0:
print(current.info," -> ", end="")
count+=1
current=current.next
print("None")
#Approach 2 using flag concept (1 and -1)
def displayAlternate2(self):
if self.head is None:
print("Empty Linked List")
return
else:
display=1
current=self.head
while current:
if display==1:
print(current.info," -> ", end="")
display=display*(-1)
current=current.next
print("None")
#Approach 3 using pointer move
def displayAlternate3(self):
if self.head is None:
print("Empty Linked List")
return
else:
current=self.head
while current:
print(current.info," -> ", end="")
if current.next: # Check if we can skip one node
current = current.next.next
else: # We are at the last node of an odd-length list
current = None
print("None")
mylist=LinkedList()
n=int(input("How many elements you want to insert"))
for i in range(n):
ele=int(input("Enter element"))
mylist.insbeg(ele)
print("My linked list")
mylist.display()
print("Alternative Elements using Approach I ")
mylist.displayAlternate1()
print("Alternative Elements using Approach II ")
mylist.displayAlternate2()
print("Alternative Elements using Approach III ")
mylist.displayAlternate3()