Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created mergedlinkedlist #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions String/mergelinkedlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#class definition
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None


def mergeLinkedLists(headOne, headTwo):
p1 = headOne #storing references in variables
p2 = headTwo
prev = None

while p1 and p2: #until p1 and p2 are not null
if p1.value < p2.value: #checking for less value of node
prev = p1 #storing it to keep track of linkedlist
p1 = p1.next #point to nextnode
else:
if prev: #if prev is not null
prev.next = p2
prev = p2
p2 = p2.next
prev.next = p1

if p1 is None: #if p1 is null then point prev next to p2
prev.next = p2

return headOne if headOne.value < headTwo.value else headTwo