Skip to content

Commit 6de43e8

Browse files
Merge pull request raviprakashdev#14 from tanseersaji/tanseersaji_pr
Added Implementation of LinkedList DS
2 parents fb8ee2e + 9be7a93 commit 6de43e8

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

algorithms/LinkedList.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Author: Tanseer Saji <github.com/tanseersaji>
3+
Desc: An Implementation of Linked List Data Structure
4+
"""
5+
6+
7+
class Node:
8+
def __init__(self, val=0, next=None) -> None:
9+
self.val = val
10+
self.next = next
11+
12+
13+
def search(root, val) -> bool:
14+
ptr = root
15+
while ptr:
16+
if ptr.val == val:
17+
return True
18+
ptr = ptr.next
19+
return False
20+
21+
22+
def traverse(root) -> None:
23+
ptr = root
24+
while ptr:
25+
print(ptr.val)
26+
ptr = ptr.next
27+
28+
29+
def insert(root, val) -> None:
30+
ptr = root
31+
while ptr.next:
32+
ptr = ptr.next
33+
ptr.next = Node(val)
34+
35+
36+
def delete(root, val) -> None:
37+
ptr = root
38+
if ptr.val == val:
39+
root = ptr.next
40+
ptr = None
41+
return
42+
43+
prev = None
44+
while ptr:
45+
if ptr.val == val:
46+
break
47+
prev = ptr
48+
ptr = ptr.next
49+
if not ptr:
50+
return
51+
52+
prev.next = ptr.next
53+
ptr = None
54+
55+
56+
if __name__ == "__main__":
57+
llist = Node(0)
58+
for i in range(1, 10):
59+
insert(llist, i)
60+
delete(llist, 1)
61+
traverse(llist)

algorithms/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This folder contains
2+
# Implementation of DS & Algo in Python

0 commit comments

Comments
 (0)