Skip to content

Commit eb8e55e

Browse files
HackerRank --> Data Structure --> Linked Lists
1 parent eb1899f commit eb8e55e

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,152 @@
601601
" display(merge(l1,l2))"
602602
]
603603
},
604+
{
605+
"cell_type": "markdown",
606+
"metadata": {},
607+
"source": [
608+
"# remove duplicate"
609+
]
610+
},
611+
{
612+
"cell_type": "code",
613+
"execution_count": 8,
614+
"metadata": {},
615+
"outputs": [],
616+
"source": [
617+
"class node:\n",
618+
" def __init__(self,data):\n",
619+
" self.data=data\n",
620+
" self.next=None\n",
621+
"class LinkedList:\n",
622+
" def __init__(self):\n",
623+
" self.start=None\n",
624+
"def display(head):\n",
625+
" if head==None:\n",
626+
" print('list is empty')\n",
627+
" else:\n",
628+
" temp=head\n",
629+
" while temp:\n",
630+
" print(temp.data)\n",
631+
" temp=temp.next\n",
632+
"def addElemntInLinkedList():\n",
633+
" l=LinkedList()\n",
634+
" for i in range(int(input())):\n",
635+
" n=int(input())\n",
636+
" if l .start==None:\n",
637+
" l.start=node(n)\n",
638+
" ptr=l.start\n",
639+
" else:\n",
640+
" ptr.next=node(n)\n",
641+
" ptr=ptr.next\n",
642+
" return l.start"
643+
]
644+
},
645+
{
646+
"cell_type": "code",
647+
"execution_count": 49,
648+
"metadata": {},
649+
"outputs": [
650+
{
651+
"name": "stdin",
652+
"output_type": "stream",
653+
"text": [
654+
" 5\n",
655+
" 2\n",
656+
" 2\n",
657+
" 4\n",
658+
" 4\n",
659+
" 6\n"
660+
]
661+
}
662+
],
663+
"source": [
664+
"head=addElemntInLinkedList()"
665+
]
666+
},
667+
{
668+
"cell_type": "code",
669+
"execution_count": 50,
670+
"metadata": {},
671+
"outputs": [
672+
{
673+
"name": "stdout",
674+
"output_type": "stream",
675+
"text": [
676+
"2\n",
677+
"2\n",
678+
"4\n",
679+
"4\n",
680+
"6\n"
681+
]
682+
}
683+
],
684+
"source": [
685+
"display(head)"
686+
]
687+
},
688+
{
689+
"cell_type": "code",
690+
"execution_count": 51,
691+
"metadata": {},
692+
"outputs": [],
693+
"source": [
694+
"def remove_duplicate(head):\n",
695+
" if head is None:\n",
696+
" return\n",
697+
" else:\n",
698+
" ptr=head\n",
699+
" temp=head.data\n",
700+
" head_d=node(head.data)\n",
701+
" ptr_d=head_d\n",
702+
" while ptr:\n",
703+
" if temp!=ptr.data:\n",
704+
" temp=ptr.data\n",
705+
" ptr_d.next=node(ptr.data)\n",
706+
" ptr_d=ptr_d.next\n",
707+
" ptr=ptr.next\n",
708+
" return head_d"
709+
]
710+
},
711+
{
712+
"cell_type": "code",
713+
"execution_count": 52,
714+
"metadata": {},
715+
"outputs": [
716+
{
717+
"data": {
718+
"text/plain": [
719+
"<__main__.node at 0x7f1a7e573f70>"
720+
]
721+
},
722+
"execution_count": 52,
723+
"metadata": {},
724+
"output_type": "execute_result"
725+
}
726+
],
727+
"source": [
728+
"remove_duplicate(head)"
729+
]
730+
},
731+
{
732+
"cell_type": "code",
733+
"execution_count": 53,
734+
"metadata": {},
735+
"outputs": [
736+
{
737+
"name": "stdout",
738+
"output_type": "stream",
739+
"text": [
740+
"2\n",
741+
"4\n",
742+
"6\n"
743+
]
744+
}
745+
],
746+
"source": [
747+
"display(remove_duplicate(head))"
748+
]
749+
},
604750
{
605751
"cell_type": "code",
606752
"execution_count": null,

0 commit comments

Comments
 (0)