Enull: Can We Delete A Node Given The Node Itself As Parameter?
Enull: Can We Delete A Node Given The Node Itself As Parameter?
itself as parameter?
Is there any efficient way, provided that the given
node is not the last node?
⑬ - +
+ enull
a a next
a next a
=
next next
⑬
- +
N s-- -
node
null
nodenext
node.val=node.next.val - error
2 3 4 5
& 1
1 2 34S 6
6 S 4 3 2 I
m 6
=
⑱ - - - - null
slow fast
head
(fast!=null) [
I
while
Node slow head;
slow:Slow, next;
Node fast head;
-
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
slow, next:
slow, next.next;
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
-> if (fast==null)[
head: head, next;
return;
3
5s,
b
⑳- -
-- 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 slow.next;
Slow:
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
6 Slow
e null
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;
->
⑪-- 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;
3
return false;
③ _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
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 temp.next;
slow, next;
slow:
53 10 3
6 Node
temp head; 11 return slow;
=
What do you think?
head1
enull
- - nue
Lo
->
- 0
⑱
-
-
temp
**
Merge two sorted linked lists (Without extra
t, t,
=
next
n
② - -
null
t -
Nodet h; 3
=
if (t2 =
null)[
=
12 t.next: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
-
-
⑰De-Sen
t tr
① -
+ -null +
④Dede ene
new head
-01 - interchange,
8
-
2.5) null
3) return the 3
- - -
0 - Q -
2) h. next.next=h
snull
⑤ D- +-null
-
⑪-
-
8 00 Y
- = = ->
h
-8 - 0 =
0x4
mul
& &=
null-Base
num
case
③ -
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
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]
+
0 0 + 6+6 feenull
+ + +
new Left
->
enull
reverse (-)
middle
node
6+ ene
90 1
+
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
=
next.next;
- t2 t 2=
next.next;
Spy Separating the lists
get nuc
⑧
small
43
t
t 1.next=t2. next;
t 1 t1. next
=