0% found this document useful (0 votes)
29 views37 pages

Linked List 04 - Class Notes - (DECODE DSA With C++ 2.0)

The document discusses the limitations of singly linked lists and introduces the concept of doubly linked lists, highlighting their advantages in terms of insertion and deletion efficiency. It covers the implementation details, including node structure and methods for inserting and deleting nodes at various positions. Additionally, it addresses various linked list problems and algorithms, including splitting linked lists and copying lists with random pointers.

Uploaded by

Vinayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views37 pages

Linked List 04 - Class Notes - (DECODE DSA With C++ 2.0)

The document discusses the limitations of singly linked lists and introduces the concept of doubly linked lists, highlighting their advantages in terms of insertion and deletion efficiency. It covers the implementation details, including node structure and methods for inserting and deleting nodes at various positions. Additionally, it addresses various linked list problems and algorithms, including splitting linked lists and copying lists with random pointers.

Uploaded by

Vinayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Linked List

Part - 4

Raghav Garg
Revisiting the limitations of “Singly Linked
List”
⑪ - - - -
Nich

T.C.
of get, delete

mode, can back


particular then never
ata you go
·
If you are

linked list
Station:Doubly
Introducing Doubly Linked List
Can we make our insertion and deletion more
efficient?
A
x 400
two t
peer next
*

val
*

↓ ↓

stat
400 XI00 x 250 x 800
x

tail
head
Introducing Doubly Linked List
Node Class class Node [

int val;
Node*next;
Node* puev;

Node lint val)[


val;

I
this - val=

this is next NULL;


=

NULL;
this eprer=
3

3
Note that we take up more memory
Implementation
Insertion and Deletion

nurN- Null
Implementation
Insertion and Deletion
some befits of Body Id it:

order. (via Tail model


1) If you want
to
display 11 in reverse

n
% NurL

display/nead)4 display/nead)4
(head==mull) return;
1 if (head==will)
val;
return; if
head (head next);
display
-
cont -

val;
display/headenext); cont head -

3
3
reverse
normal
2) If any
mode
of list is given, we can traverse through
the entire list tail

was t
- i

next? NULL)
while (t
=
e

Iteprev!=NULL)d
while
I t t =
e next;
1 t tepwer;
=

3
3 t
Nodet tail =

Node* ti
head:
Doubly s
Insert (tail, head, index)
1)

2) delete (tail,

head, index
0(1) O(1 o(us

head, index (
3) get
(tail,
Insert At Tail
nee

Nun
- Nuch

insert AtTail (50);

Nun NULL

Create Node temp


*
1) temp
2) tail - temp
next: tail

tail
3) temp eprev=

a) tail temp
=
=Head
Nun
- Nuch

AtHead
insert (SO)

Nun
- Nuch

⑮ 1) Create new node temp


temp head
next:
head 2) temp
-

3) head - temp
prew=

4) t
head: emp
Index
I

run NdJorge Nurn

insert At Idx (2, 501 ;

run irgenurn -

tenext: temp e next templat


t
t
-next:
temp
t + temp
puer:
tenext eprev= t
Head:
A

riggrerunn
head,
head: next;

NULL) head NULL;


puer=
if(head!
-
& =

NrLL) tail NULL;


(head
=

if
==

NUL
-- NoL
head
tail
*
DeTail (Better than SLL)

win, Ire NULL

temp
tail
Nodet temp
=

tail-puer
NULL;
enext=
temp
tail: temp;
Index
Det

runs"

head
->

inreferen
temp-next
delete AtIndex (2);

head
1) temp
=

to idx-1
2) traverse temp
-next-next
3) temp
tempenext:
4) temp -next t
eprer: emp
GeIndex (Oised)

have DLL of size100 & head, tail, size


1) a
·postatement: You
have delete index
sat element
2) You to

Node*temp head.
MI:
=

traverse temp idx time

Nodet temp tail;


:
=

(100-idt) times
traverce temp
Other types of Linked List Node* temps:head;

while (temp-next! head)


-

Sch I temp temp


=
-> next;
3
temp t

⑱ Node*t newNode(val);
=

NULL

tempsnext=t,
S
head;
tenext:

Nodeta dd (Nodey head, int val([


-

3
Other types of Linked List
fail
2) Circular Doubly 2L
head


-en

fail
t
At
insert Tail (S0) head

wu
-

tail t
snext: :

tail;
tepwev:
t -next:h ead;

head-prer-t;
tail:t
Ques: Split Linked List in Parts [Leetcode - 725]

asul k
=
3

21 3
=

①-
me
141810-Q1NN ee E3

① - - N
Vector Listwode"> v

③ e018 - N

⑨ De DeN
Ques: Split Linked List in Parts [Leetcode - 725]
temp
⑨ - -
- 6181810-NIN
tod R3


=

~O
I

no,
of parts
0
int n
rem= YK

11%3
=

F2
=

ese parts janke


Length of parts I, 1,0
=

length long a

=3 vock
I extra logi
Ques: Find the Minimum and Maximum Number of Nodes
between Critical Points [Leetcode - 2058]

%ser C

FFMAX
mind= B & 1
fidx= Sidx
fidx=+ * * Y
sidx=idX
sidx=-1&M5
intd B & 1
=

S-2 3
max sidyfidx=
distance:
=

fidx=*2
Sidx=#14 S
Ques: Reverse Nodes in Even Length Groups [Leetcode - 2074]

DeGen
temp
gap=XR BY

reverse bw (temp, 2,2+gap);

-
-
Ques: Reverse Nodes in Even Length Groups [Leetcode - 2074]

02-815-618-8-9
--
t
temp
remlen-1
if (remlen <gap+1) gap=
Ques: Copy List with Random Pointer [Leetcode - 138]
See: Create a
deep copy (without
random)
class Node (

I
i int val;

-Nurs Nodetnext;
Node* random;

3
--

-
⑦ ⑪ Newurz
-
Ques: Copy List with Random Pointer [Leetcode - 138]
Step-2:Create alternate connections (mergel

[7, N1, 43, 77, 41, 17,20,117


Ad
[1,7]
wr
⑦ -
*
⑦ wich
d
Ques: Copy List with Random Pointer [Leetcode - 138]
Step Assigning
3: random pointer of duplicate
[7, N1, 43, 77, 41, 17,20,117

·get
[1,7]
NULL

⑲Newu
t2 the next
=

the random next


if (tstrandom)t2 random =
+

t 1 tienexte next
=
Ques: Copy List with Random Pointer [Leetcode - 138]
the lints
Step-separating

·gener
⑦-
NULL

e
⑦ -
-
+ N18-9sNuL
Ques: Copy List with Random Pointer [Leetcode - 138]
the lints
Step-separating

un
·No

al

OraNu
d


-
next t
t
tenext: 12 +

t = t next NULL
tenext:
t = t + next
next +2 t2enext
=
12 NULL
enext=
t H
=
+
Ques: Copy List with Random Pointer [Leetcode - 138]

⑦30 NULL

/ wurz

Ques: Copy List with Random Pointer [Leetcode - 138]


head

⑪esNu Id
dummy
Ques: Copy List with Random Pointer [Leetcode - 138]

-rus
D-Nu
t2 tle
= next
random=
+- random, next
if N4LL)
(Al-random!=
the

t1 He next
= - next
Ques: Flatten a Multilevel Doubly Linked List [Leetcode - 430]
t

nux -
- Nu

e ↓
nur
- 8N Null

wurd - NULL

#Hint:
Recursion
Ques: Flatten a Multilevel Doubly Linked List [Leetcode - 430]

1858
t

ewe
-
we -

e

C

a tenext
=

while (cenext)
c
t-child;
=

c ce
= next
c flatten
= (C);
N
child: ULL an
to
cenext=
#Hint:
tenext= c
a eprev 2
=

Recursion
t
cepuer=
Ques: Flatten a Multilevel Doubly Linked List [Leetcode - 430]

nur
- 8N Null

wurd - NULL

N-8N-N
Ques: Flatten a Multilevel Doubly Linked List [Leetcode - 430]
t a

N ① N

↑ ↑


flat

See
Next Lecture

Introduction to Stacks! & Queues

You might also like