Linked List 04 - Class Notes - (DECODE DSA With C++ 2.0)
Linked List 04 - Class Notes - (DECODE DSA With C++ 2.0)
Part - 4
Raghav Garg
Revisiting the limitations of “Singly Linked
List”
⑪ - - - -
Nich
T.C.
of get, delete
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;
I
this - val=
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:
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
Nun NULL
tail
3) temp eprev=
a) tail temp
=
=Head
Nun
- Nuch
AtHead
insert (SO)
Nun
- Nuch
3) head - temp
prew=
4) t
head: emp
Index
I
run irgenurn -
riggrerunn
head,
head: next;
if
==
NUL
-- NoL
head
tail
*
DeTail (Better than SLL)
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)
Node*temp head.
MI:
=
(100-idt) times
traverce temp
Other types of Linked List Node* temps:head;
⑱ Node*t newNode(val);
=
NULL
tempsnext=t,
S
head;
tenext:
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
=
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
-
-
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
·get
[1,7]
NULL
⑲Newu
t2 the next
=
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