-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
86 lines (70 loc) · 1.83 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
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
class Node:
def __init__(self,data,next=None):
self.data=data
self.next=next
class LinkedList:
def __init__(self):
self.head=None
def insert_data(self,data):
node=Node(data,self.head)
self.head=node
def print_data(self):
itr=self.head
llist=''
while itr:
llist+=str(itr.data) + ' ------> '
itr=itr.next
print(llist)
def insert_at_end(self,data):
if self.head is None:
self.head=Node(data,None)
return
itr=self.head
while itr.next:
itr=itr.next
itr.next = Node(data,None)
def insert_new_link(self,data):
self.head=None
for d in data:
print(self.insert_at_end(d))
def find_length(self):
count=0
itr=self.head
while itr:
count+=1
itr=itr.next
return count
def remove(self,index):
if index<0 or index>self.find_length():
return False
prev=None
curr=self.head
count=0
while curr and count!=index:
prev=curr
curr=curr.next
count+=1
prev.next=curr.next
curr=None
def swap_node(self,key1,key2):
if key1==key2:
return False
prev=None
curr=self.head
while curr and curr.data!=key1:
prev=curr
curr=curr.next
prev_1=None
curr_1=self.head
while curr_1 and curr_1.data!=key2:
prev_1=curr_1
curr_1=curr_1.next
if prev:
prev.next=curr_1
else:
self.head=curr_1
if prev_1:
prev_1.next=curr
else:
self.head = curr
curr.next,curr_1.next = curr_1.next,curr.next