DEV Community

Lancelot03
Lancelot03

Posted on

1 1

Linked List Part-2

Linked List Insertion at the front--

Insertion at the front

class Node:
    def __init__(self, data):
        self.data=data
        self.ref=None 
class LinkedList:
    def __int__(self):
        self.head=None

    def begin(self,new_data):    
        q1=Node(new_data)
        q1.ref=self.head
        self.head=q1
Enter fullscreen mode Exit fullscreen mode

Insertion at the Middle--

Insertion at the Middle

def inbet(self,prev,new_data):
        if prev.ref is None:
            print('LL is empty')
            return
        q4 = Node(new_data)
        q4.ref=prev.ref
        prev.ref=q4
Enter fullscreen mode Exit fullscreen mode

Insertion at the End--

Insertion at the End

def last(self,new_data):
        q2=Node(new_data)
        if self.head==None:
            self.head=q2
            return
        temp=self.head
        while(temp.ref):
            temp=temp.ref

        temp.ref=q2
Enter fullscreen mode Exit fullscreen mode

Printing Of Linked List--

def print_ll(self):
        if self.head is None:
            print("Linked list is empty")
            return
        n=self.head
        while n:
            print(n.data)
            n=n.ref
Enter fullscreen mode Exit fullscreen mode

Function Calling--

h1=Node(2)
h2=Node(3)

k1=LinkedList()
k1.head=h1
h1.ref=h2

k1.begin(1)
k1.inbet(k1.head.ref,5)
k1.last(8)

k1.print_ll()
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay