Can we delete a node given the node
itself as parameter?
Is there any efficient way, provided that the given
node is not the last node?
⑬ - +
+ enull
a a next
a data anext data
=
a next a
=
next next
⑬
- +
N s-- -
node
null
nodenext
[Link]=[Link] - error
node, next= node, next. next
Finding Nth Node from the end of Linked
List [Only head is given] return mode;(LeetCode)
⑱ -
-
-
-
null
2 3 4 5
& 1
1 2 34S 6
6 S 4 3 2 I
m 6
=
nth from last (m-n+1*
=
node from start
Finding Nth Node from the end of Linked
List (In traversal)
one Ex:2nd mode from last
⑱ - - - - null
slow fast
head
(fast!=null) [
I
while
Node slow head;
slow:Slow, next;
Node fast head;
-
fast: fast, next;
for lint i = 1; i <
n;i +3
= +
3
I fast: fast, next; return
slow;
3
Removing Nth Node from the end of Linked
List (in traversal)
one node from last n 2
=
nd
⑱ - -
-null
head slow fast
slow, next:
slow, [Link];
Removing Nth Node from the end of Linked
List mtnode from end (Where
n =
of nodes) m no.
=
Head Node
deteleNthFramEnd (a, 6/
a
100 +
b
13+
4e52-fonner
c
100 6 head fast
Slow
-> if (fast==null)[
head: head, next;
return;
3
5s,
b
1006 100 -> 1eYe nu
head
fast
a
Slow
Finding intersection of two linked lists
Two pointers to the rescue…
⑳- -
-- nul
-
⑩
-
head 2
Lemp2
# Hint:Find the
lengths of both lists
#Hint
-
-
2:increment
the
bigger list
by (m-n)->
steps small
length of
length of list
big list
Finding middle element of a linked list
⑱ nl
-
-
- -
-
-
slow fast
↓
right
middle
#Hint: 1)
-
Slow &
fast while (fast!Inull) (
2) fast - 2x [Link];
Slow:
fast:fast, next, next;
3
Finding middle element of a linked list
⑱ -
- +
N
+
-
nl
head fast
null
For Even no.
of modes fast,
- next, next!I
↓
a leftmiddle
Finding middle element of a linked list
⑤
head
- -
slow
-
8
ee e
⑤ - ne
-
8 0 + -
31 - -
- - well
head slow fast
For odd number of nodes -
fast, next!I null
Finding middle element of a linked list
① +
2 +
B 4 + +
$-nill
head slow
fast
6 Slow
e null
fast
while (fast, next!-null && fast!=null)[
slow:slow, next;
fast, next, next;
fast:
3
Deleting the middle element of the linked
list
Odd: ① - - - null
fast, next. next, next;
->
head slow fast
① - - +( + - null
&
head slow fast fast - next next next;
While ( x ① snull
-
fast. Next, next next;
I slow: slow, next; head
Slow
fast:fast, next next;
fast
3
Deleting the middle element of the linked
list Even Rightmiddle
-
-
- be deleted has to
① - - snull
fast, next next;
->
head slow fast
⑪-- nett
slow
fant fast. next next;
while ( 14
slow:slow, next;
I fast:fast, next, next;
3
①- null
head
Slow
fast
-
①- mell
head
slow
fast
Cycle in a linked list
X
while (fast!=null) [
fast
I
slow slow:slow, next;
31 0 1 fast: fast, next next;
head if (fast = = slowretran true;
3
return false;
#it1: Use slow fast
③ _8- -null
-
fast
slow
Cycle in a linked list
① - null
head slow
fast
Cycle in a linked list rof
fast
Nodes=n
① -
-
8 8
+
cycle length m
=
- men
1-n-mel - m -
sin
-
m X
+
x
->
n x
+
2(n
= -
m x]
+
f:n
+
slows I -> n x + 2n
=
-
2m 2x +
2
fast -
I 2m -n x
=
Se
Cycle in a linked list fe 3
I
① -
0 1 8 0
-
+ +
-
Practice ->
Interview
Find out the node where the cycle begins, i.e. the
node at which the tail node points.
temp
8 - 2 +
8
head I fast
slow
Cycle Detection + Number of nodes in cycle
Cycle Detection + Interesting Observation
H.W. Leetlode I
-
-
-
cycle 2-
① - 18
fast
I
tem
I while (fast!=null)[
slow, next;
slow: 7 While (temp! slow) h
I
2
-
Y
fast:fast, next, next;
if (fast== slow) break;
8
9
I temp [Link];
slow, next;
slow:
53 10 3
6 Node
temp head; 11 return slow;
=
What do you think?
Which of the following algorithms is not feasible to
implement in a linked list?
(a) Linear Search
(b) Merge Sort
(c) Insertion Sort
~
(d) Binary Search
What do you think?
Which of the following algorithms is not feasible to
implement in a linked list?
(a) Linear Search
(b) Merge Sort
(c) Insertion Sort
(d) Binary Search
*
Merge two sorted linked lists Using
-
extra
space
① -
head1
enull
- - nue
Lo
->
- 0
⑱
-
-
temp
**
Merge two sorted linked lists (Without extra
-> ③ - snull Space
in
⑩ ↓ ↑
I
t t
=
t, t,
=
next
n
② - -
null
t -
while (t!=null && t2!=null)[
Node h new
=
Node (100);
Nodet h; 3
=
if (t2 =
null)[
=
12 [Link]:t 1;
n
(H.w.
Practice consider)
Given a linked list, split it into two lists such that one
contains odd values, while the other contains even
values.
3 6 N- 2180- null
- 1
temp
-
⑪ -
- -
0 -
0
tempo
-
-
2-8-
tempe
Practice & Homework
Given a linked list, split it into two lists such that one
contains odd values, while the other contains even
values. h - -
-
men
③ - ④ - &8- nu
-
null
- tempe
temp even
Odd
Practice Homework
-
-
Remove duplicates from a sorted linked list.
h
⑰De-Sen
t tr
① -
+ -null +
if (temp. [Link] [Link])
=
=
Recursion for Reversal O(n) -TC
S.C. eo(n)
Reverse a linked list and return its new head.
③ - - D-2- 4-null
head
④Dede ene
new head
display(), revdisplay (Node head);
0 +N-null
8
W
- +
-
1) head, next
isko new Kando
this will get
reversed
2) head, head, next
-01 - interchange,
8
-
2.5) null
3) return the 3
new head call stack
- - -
0 - Q -
2) h. [Link]=h
snull
⑤ D- +-null
-
⑪-
-
8 00 Y
- = = ->
h
-8 - 0 =
0x4
mul
& &=
null-Base
num
case
if ([Link]== null) returnh;
&
⑳
84024
&
③ -
X
aben
-
~a reverse
=
(a);
X 2
3 e
~ ~
⑧ Y S a vin Y
V. 3 ~ Yo 2
~3 ~2.
- Y v Y
A ↑
5 f
~ v
-
& u 1 ⑳ Ye2e 2
v ↑ · next 5
=
v 2 o next= 1
~ 5 I v
v y v U
Can we reverse a linked list without
recursion? prev, curr, agla
Are two pointers enough?... 1)
agla = cur next
=null 2) curr. next prev
L
=
agla prev
carr
3) pwer=curs
w
4) r=
agla
prev curu
agla
Palindrome Linked List
Check whether a linked list is palindrome or not?
public static boolean is Palindrome (Node head) [
3
⑰
List 2--B-Q+
⑳ad* Ismell
Duplicate -- -- Denull
C
e 0-2 84 8 & 0 - -
-
n
Palindrome Linked List
Check whether a linked list is palindrome or not?
Ser: ---- Denull 1) Left middle
2) Right half ko
① - -
Deeme reverse
Ps p1, p2
3)
0 2 -8 3 +
- - +
① -
PL
- -
0
Palindrome Linked List
Check whether a linked list is palindrome or not?
8- nill
① - - - -
8+
S
fast next, next
⑪ - - -
- Senull
A fast, next.
S
Practice [Leetcode 2130]
+
Find the maximum twin sum of a linked list of even
length.
Twin of any node at (i)th index is the node at
(n-i-1)th index. Twin sum is the sum of values of a
node and its twin.
0 0 + 6+6 feenull
+ + +
new Left
->
enull
reverse (-)
middle
node
6+ ene
90 1
+
int Integer. MIN_VALUE;
max:
de328:Odd Even Linked List
ever-
⑪ ⑤ envel
tempe temp
odd &
⑩ ① ⑥
2
tempo
neach
even
⑧genull
neaded 2
↑
temp
⑳
**
Practice
Copy List with Random Pointer: Construct a
deep copy of a linked list where each node
contains an additional random pointer, which
could point to any node in the list or null.
An interesting question!
1:Deep
Ste copy
Step2:Connect the lists
S-2 Deep copy -
(
heads
D- nuc
B NN-Denull
⑧ ⑦ -
h
S-2
D-nuc
Alternate
connections
head
t1
0 I***
N Demull
⑦
43
t2
t
t2
t next:
text,e etern
Se3 Assigning Random pointers
D- nuc
⑧ ↓ t2 t1
Denull
43
t
t n3
if (t1. random=null) t2. zandom= null
=
Xt2. random= t 1. random. Next,
while
-> & 1 t 1.
=
[Link];
- t2 t 2=
[Link];
Spy Separating the lists
get nuc
⑧
small
43
t
t [Link]=t2. next;
t 1 t1. next
=
t2. next: t1. next
t2.
t2 = next;
gen
⑧-
head2 temp 2
temp1= tem 1 next;