Skip to content

Commit 312fa3a

Browse files
Merge-two-sorted-linked-lists
1 parent 20c7666 commit 312fa3a

File tree

2 files changed

+162
-4
lines changed

2 files changed

+162
-4
lines changed

HackerRank/Data Structure/Linked Lists/Linked-Lists.ipynb

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,109 @@
496496
" print(compare(l1,l2))"
497497
]
498498
},
499+
{
500+
"cell_type": "markdown",
501+
"metadata": {},
502+
"source": [
503+
"# Merge two sorted linked lists\n"
504+
]
505+
},
499506
{
500507
"cell_type": "code",
501-
"execution_count": 38,
508+
"execution_count": 18,
502509
"metadata": {},
503-
"outputs": [],
510+
"outputs": [
511+
{
512+
"name": "stdin",
513+
"output_type": "stream",
514+
"text": [
515+
" 1\n",
516+
" 3\n",
517+
" 1\n",
518+
" 2\n",
519+
" 3\n",
520+
" 2\n",
521+
" 3\n",
522+
" 4\n"
523+
]
524+
},
525+
{
526+
"name": "stdout",
527+
"output_type": "stream",
528+
"text": [
529+
"1\n",
530+
"2\n",
531+
"3\n",
532+
"3\n",
533+
"4\n"
534+
]
535+
}
536+
],
504537
"source": [
505-
"if 3 and None:\n",
506-
" print('k')"
538+
"# %load Merge-two-sorted-linked-lists.py\n",
539+
"class node:\n",
540+
" def __init__(self,data):\n",
541+
" self.data=data\n",
542+
" self.next=None\n",
543+
"class LinkedList:\n",
544+
" def __init__(self):\n",
545+
" self.start=None\n",
546+
"def display(head):\n",
547+
" if head==None:\n",
548+
" print('list is empty')\n",
549+
" else:\n",
550+
" temp=head\n",
551+
" while temp:\n",
552+
" print(temp.data)\n",
553+
" temp=temp.next\n",
554+
"\n",
555+
"def merge(l1,l2):\n",
556+
" l=LinkedList()\n",
557+
" ptr=l\n",
558+
" while l1 or l2:\n",
559+
" if l1 is None:\n",
560+
" ptr.next=l2\n",
561+
" break\n",
562+
" elif l2 is None:\n",
563+
" ptr.next=l1\n",
564+
" break\n",
565+
" else:\n",
566+
" if l1.data>l2.data:\n",
567+
" ptr.next=l2\n",
568+
" l2=l2.next\n",
569+
" else:\n",
570+
" ptr.next=l1\n",
571+
" l1=l1.next\n",
572+
" ptr=ptr.next\n",
573+
" return l.next\n",
574+
"# if l1 is None and l2 is None:\n",
575+
"# return None\n",
576+
"# if l1 is None:\n",
577+
"# return l2\n",
578+
"# if l2 is None:\n",
579+
"# return l1\n",
580+
"# if l1.data>l2.data:\n",
581+
"# l=l2\n",
582+
"# l.next=merge(l1,l2.next)\n",
583+
"# else:\n",
584+
"# l=l1\n",
585+
"# l.next=merge(l1.next,l2)\n",
586+
"# return l\n",
587+
"def addElemntInLinkedList():\n",
588+
" l=LinkedList()\n",
589+
" for i in range(int(input())):\n",
590+
" n=int(input())\n",
591+
" if l .start==None:\n",
592+
" l.start=node(n)\n",
593+
" ptr=l.start\n",
594+
" else:\n",
595+
" ptr.next=node(n)\n",
596+
" ptr=ptr.next\n",
597+
" return l.start\n",
598+
"for _ in range(int(input())):\n",
599+
" l1=addElemntInLinkedList()\n",
600+
" l2=addElemntInLinkedList()\n",
601+
" display(merge(l1,l2))"
507602
]
508603
},
509604
{
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class node:
2+
def __init__(self,data):
3+
self.data=data
4+
self.next=None
5+
class LinkedList:
6+
def __init__(self):
7+
self.start=None
8+
def display(head):
9+
if head==None:
10+
print('list is empty')
11+
else:
12+
temp=head
13+
while temp:
14+
print(temp.data)
15+
temp=temp.next
16+
17+
def merge(l1,l2):
18+
l=LinkedList()
19+
ptr=l
20+
while l1 or l2:
21+
if l1 is None:
22+
ptr.next=l2
23+
break
24+
elif l2 is None:
25+
ptr.next=l1
26+
break
27+
else:
28+
if l1.data>l2.data:
29+
ptr.next=l2
30+
l2=l2.next
31+
else:
32+
ptr.next=l1
33+
l1=l1.next
34+
ptr=ptr.next
35+
return l.next
36+
# if l1 is None and l2 is None:
37+
# return None
38+
# if l1 is None:
39+
# return l2
40+
# if l2 is None:
41+
# return l1
42+
# if l1.data>l2.data:
43+
# l=l2
44+
# l.next=merge(l1,l2.next)
45+
# else:
46+
# l=l1
47+
# l.next=merge(l1.next,l2)
48+
# return l
49+
def addElemntInLinkedList():
50+
l=LinkedList()
51+
for i in range(int(input())):
52+
n=int(input())
53+
if l .start==None:
54+
l.start=node(n)
55+
ptr=l.start
56+
else:
57+
ptr.next=node(n)
58+
ptr=ptr.next
59+
return l.start
60+
for _ in range(int(input())):
61+
l1=addElemntInLinkedList()
62+
l2=addElemntInLinkedList()
63+
display(merge(l1,l2))

0 commit comments

Comments
 (0)