๐ฌ๐ง 141. Linked List Cycle
Hi, guys! Today we are solving Linked List problem again.
Challenge for you: Can you solve it using O(1) (i.e. constant) memory?
๐ก Medium: 141. Linked List Cycle
๐จโ๐ป My solution: Java O(1) Memory solution
Good luck!โ๏ธ
Hi, guys! Today we are solving Linked List problem again.
Challenge for you: Can you solve it using O(1) (i.e. constant) memory?
๐ก Medium: 141. Linked List Cycle
๐จโ๐ป My solution: Java O(1) Memory solution
Good luck!โ๏ธ
LeetCode
Linked List Cycle - LeetCode
Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
๐1
๐บ๐ฟ 141. Linked List Cycle
Hammaga salom! Bugun ham Linked List-ga taalluqli masala yechamiz. Bu masalada sizlar list-da cycle borligini aniqlashingiz kerak.
Sizlarga challenge: O(1) xotira (constant) ishlatgan holda yecha olasizmi?
๐ก O'rta daraja: 141. Linked List Cycle
๐จโ๐ป Mening yechimim: Java O(1) Memory solution
Omad!โ๏ธ
Hammaga salom! Bugun ham Linked List-ga taalluqli masala yechamiz. Bu masalada sizlar list-da cycle borligini aniqlashingiz kerak.
Sizlarga challenge: O(1) xotira (constant) ishlatgan holda yecha olasizmi?
๐ก O'rta daraja: 141. Linked List Cycle
๐จโ๐ป Mening yechimim: Java O(1) Memory solution
Omad!โ๏ธ
LeetCode
Linked List Cycle - LeetCode
Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
๐7
๐ฌ๐ง 141. Linked List Cycle
Hi, guys! Let's check the solution for yesterday's problem.
๐ก Medium: 141. Linked List Cycle
๐จโ๐ป My solution: Java O(1) Memory solution
๐จโ๐ป Task
Your task is to find whether a list has cycle or doesn't.
๐จโ๐ป Explanation
This problem can be solved by using The Tortoise and the Hare (Floydโs) Algorithm. [I leant about this algorithm thanks to Bobosher].
My solution was simpler. If you pay close attention to the constraints in the description, then you can see that
What does it give us? We can use a number out of this boundaries [but inside integer limits] to sign nodes which we already have gone through.
Let's take 1_000_000. This number is out of the given constraints, which means we won't face this number in test cases. Then I just loop through the list once and change values of nodes to 1_000_000. When some node's
Since I use only given list, then the memory usage is O(1).๐ค
I hope you liked the solution! ๐
Peace! โ๏ธ
Hi, guys! Let's check the solution for yesterday's problem.
๐ก Medium: 141. Linked List Cycle
๐จโ๐ป My solution: Java O(1) Memory solution
๐จโ๐ป Task
Your task is to find whether a list has cycle or doesn't.
๐จโ๐ป Explanation
This problem can be solved by using The Tortoise and the Hare (Floydโs) Algorithm. [I leant about this algorithm thanks to Bobosher].
My solution was simpler. If you pay close attention to the constraints in the description, then you can see that
Node.val is between -100_000 and 100_000. What does it give us? We can use a number out of this boundaries [but inside integer limits] to sign nodes which we already have gone through.
Let's take 1_000_000. This number is out of the given constraints, which means we won't face this number in test cases. Then I just loop through the list once and change values of nodes to 1_000_000. When some node's
val will be equal to 1_000_000, then it means that we have cycle. If while loop ends without facing such value, then the list doesn't possess a cycle.Since I use only given list, then the memory usage is O(1).๐ค
I hope you liked the solution! ๐
Peace! โ๏ธ
LeetCode
Linked List Cycle - LeetCode
Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
๐ณ2
๐บ๐ฟ 141. Linked List Cycle
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqish payti keldi.
๐ก O'rta daraja: 141. Linked List Cycle
๐จโ๐ป Mening yechimim: Java O(1) Memory solution
๐จโ๐ป Vazifa
Bizning vazifamiz list-da cycle mavjudligini topish.
๐จโ๐ป Izoh
Bu masala Toshbaqa va Quyon (Floyd) Algoritmini ishlatgan holda yechish mumkin. [Bu algoritm haqida Bobosherdan bilib oldim].
Mening yechimim oddiyroq. U vazifada berilgan elementlar qiymatlarining cheklovariga asoslangan. Agar yaxshilab vazifani qarasangiz,
Bu bizga nima beradi? Biz shu cheklovlardan tashqari [integer cheklovlari ichidagi] bitta sonni olib, uni ko'rsatgich sifatida ishlatsak bo'ladi.
Men 1_000_000 sonini oldim. Bu son berilgan cheklovlardan tashqarida. Men while loop-ning ichida har uchragan sonning qiymatini 1_000_000 o'zgartiraman. Agar iteratsiyalar bo'layotgan paytida qaysidir elementning qiymati 1_000_000 ga teng bo'lsa, cycle borligini tushunaman va
Shunday qilib, faqat berilgan list-ni ishlatganim uchun xotira ishlatilishi - O(1). ๐ค
Sizga yechim yoqdi degan umiddaman! ๐
Omad! โ๏ธ
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqish payti keldi.
๐ก O'rta daraja: 141. Linked List Cycle
๐จโ๐ป Mening yechimim: Java O(1) Memory solution
๐จโ๐ป Vazifa
Bizning vazifamiz list-da cycle mavjudligini topish.
๐จโ๐ป Izoh
Bu masala Toshbaqa va Quyon (Floyd) Algoritmini ishlatgan holda yechish mumkin. [Bu algoritm haqida Bobosherdan bilib oldim].
Mening yechimim oddiyroq. U vazifada berilgan elementlar qiymatlarining cheklovariga asoslangan. Agar yaxshilab vazifani qarasangiz,
Node.val-ning qiymati -100_000 va 100_000 orasida ekanligini ko'rasiz.Bu bizga nima beradi? Biz shu cheklovlardan tashqari [integer cheklovlari ichidagi] bitta sonni olib, uni ko'rsatgich sifatida ishlatsak bo'ladi.
Men 1_000_000 sonini oldim. Bu son berilgan cheklovlardan tashqarida. Men while loop-ning ichida har uchragan sonning qiymatini 1_000_000 o'zgartiraman. Agar iteratsiyalar bo'layotgan paytida qaysidir elementning qiymati 1_000_000 ga teng bo'lsa, cycle borligini tushunaman va
true qaytaraman. Agar bunday son topilmasdan list tugasa, u holda false qaytaraman.Shunday qilib, faqat berilgan list-ni ishlatganim uchun xotira ishlatilishi - O(1). ๐ค
Sizga yechim yoqdi degan umiddaman! ๐
Omad! โ๏ธ
LeetCode
Linked List Cycle - LeetCode
Can you solve this real interview question? Linked List Cycle - Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
There is a cycle in a linked list if there is some node in the list that can be reached again by continuouslyโฆ
๐3๐ฅฐ1
๐ฌ๐ง Theory. Linked List.
Hi, guys! Let's do some theory today. I found a youtube video where Linked Lists are explained in details. I hope you enjoy it.
Video: Linked List by Anvar aka.
His channel: SariqDev
Have a good weekend!โ๏ธ
Hi, guys! Let's do some theory today. I found a youtube video where Linked Lists are explained in details. I hope you enjoy it.
Video: Linked List by Anvar aka.
His channel: SariqDev
Have a good weekend!โ๏ธ
YouTube
#05 ALGORITMLAR | LINKED LISTS
Linked Lists ma'lumotlar tuzilmasi bilan tanishamiz.
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
๐3
๐บ๐ฟ Nazariya. Linked List.
Hammaga salom! Keling bugun nazariya o'rganaylik. Linked list haqida Anvar aka yozgan youtube-da video topdim. Bu videoda shu ma'lumotlar tuzilmasini mukammal tushuntirgan ekanlar. Men darsi yoqdi. Sizga ham yoqadi degan umiddaman.
Havola: Linked List.
Anvar akaning telegram kanali: SariqDev
Dam olish kunlarini yaxshi o'tkazing! โ๏ธ
Hammaga salom! Keling bugun nazariya o'rganaylik. Linked list haqida Anvar aka yozgan youtube-da video topdim. Bu videoda shu ma'lumotlar tuzilmasini mukammal tushuntirgan ekanlar. Men darsi yoqdi. Sizga ham yoqadi degan umiddaman.
Havola: Linked List.
Anvar akaning telegram kanali: SariqDev
Dam olish kunlarini yaxshi o'tkazing! โ๏ธ
YouTube
#05 ALGORITMLAR | LINKED LISTS
Linked Lists ma'lumotlar tuzilmasi bilan tanishamiz.
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
๐9
๐ฌ๐ง Linked List.
Hi, guys! This is the continuation of the previous post on Linked Lists.
In this video Anvar aka shows how to write your own Linked List.
Video: Linked List.
Cheers!โ๏ธ
Hi, guys! This is the continuation of the previous post on Linked Lists.
In this video Anvar aka shows how to write your own Linked List.
Video: Linked List.
Cheers!โ๏ธ
YouTube
#06 ALGORITMLAR | LINKED LISTS. AMALIYOT
Pythonda Linked Lists yaratishni o'rganamiz.
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
GitHub: https://github.com/anvarnarz/algoritm-darslar
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
GitHub: https://github.com/anvarnarz/algoritm-darslar
๐3
๐บ๐ฟ Linked List.
Hammaga salom! Oxirgi postning davomi sifatida Anvar akaning keyingi videosiga havola qoldiraman.
Bu darsda ular o'z Linked List-imizni qanday qilib yozishni ko'rsatadilar.
Havola: Linked List.
Omad! โ๏ธ
Hammaga salom! Oxirgi postning davomi sifatida Anvar akaning keyingi videosiga havola qoldiraman.
Bu darsda ular o'z Linked List-imizni qanday qilib yozishni ko'rsatadilar.
Havola: Linked List.
Omad! โ๏ธ
YouTube
#06 ALGORITMLAR | LINKED LISTS. AMALIYOT
Pythonda Linked Lists yaratishni o'rganamiz.
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
GitHub: https://github.com/anvarnarz/algoritm-darslar
Telegram: https://t.me/sariqdev
Sahifamiz: https://python.sariq.dev
Kanalimiz: https://www.youtube.com/sariqdev
GitHub: https://github.com/anvarnarz/algoritm-darslar
๐8
๐ฌ๐ง 206. Reverse Linked List
Hi, guys! Let's write some code. Today we should reverse the linked list. Try to write O(N) solution.
๐ข Easy: 206. Reverse Linked List
๐จโ๐ป My solution: Java | 100% faster | my O(n) solution
Explanation to be provided as usual.
Good luck! โ๏ธ๐ค
Hi, guys! Let's write some code. Today we should reverse the linked list. Try to write O(N) solution.
๐ข Easy: 206. Reverse Linked List
๐จโ๐ป My solution: Java | 100% faster | my O(n) solution
Explanation to be provided as usual.
Good luck! โ๏ธ๐ค
LeetCode
Reverse Linked List - LeetCode
Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
๐4
๐บ๐ฟ 206. Reverse Linked List
Hammaga salom. Keling bugun code yozaylik. Keyingi masaladagi vazifa - bu Linked List-ni orqadan oldinga bo'lgan tartibda chiqarish.
๐ข Oson daraja: 206. Reverse Linked List
๐จโ๐ป Mening yechimim: Java | 100% faster | my O(n) solution
Har doimgidek keyingi postlarda tahlil qilamiz.
Omad! โ๏ธ๐ค
Hammaga salom. Keling bugun code yozaylik. Keyingi masaladagi vazifa - bu Linked List-ni orqadan oldinga bo'lgan tartibda chiqarish.
๐ข Oson daraja: 206. Reverse Linked List
๐จโ๐ป Mening yechimim: Java | 100% faster | my O(n) solution
Har doimgidek keyingi postlarda tahlil qilamiz.
Omad! โ๏ธ๐ค
LeetCode
Reverse Linked List - LeetCode
Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
๐6
๐ฌ๐ง 206. Reverse Linked List
Hi, guys! Let's analyze the solution.
๐ข Easy: 206. Reverse Linked List
๐จโ๐ป My solution: Java | 100% faster | my O(n) solution
๐จโ๐ป Task
Our task is to reverse the Linked List.
๐จโ๐ป Explanation
As you can see I use recursion.
The basic idea is to change pointers: make pointer to point to previous nodes.
Base case 1: check if the node is null.
Base case 2: when we reach the last node, just make the last one point to the previous and return the last node.
I keep a pointer to the next node in
And, finally, I repeat the actions recursively by passing current as previous and
I hope I made it clear.
Cheers!โ๏ธ๐ค
Hi, guys! Let's analyze the solution.
๐ข Easy: 206. Reverse Linked List
๐จโ๐ป My solution: Java | 100% faster | my O(n) solution
๐จโ๐ป Task
Our task is to reverse the Linked List.
๐จโ๐ป Explanation
As you can see I use recursion.
The basic idea is to change pointers: make pointer to point to previous nodes.
Base case 1: check if the node is null.
Base case 2: when we reach the last node, just make the last one point to the previous and return the last node.
I keep a pointer to the next node in
tempNext, then make current node point to the previous by head.next = prev.And, finally, I repeat the actions recursively by passing current as previous and
tempNext as current for next iteration.I hope I made it clear.
Cheers!โ๏ธ๐ค
LeetCode
Reverse Linked List - LeetCode
Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
๐2๐2
๐บ๐ฟ 206. Reverse Linked List
Hammaga salom. Keling shu masalaning yechimini ko'rib chiqamiz.
๐ข Oson daraja: 206. Reverse Linked List
๐จโ๐ป Mening yechimim: Java | 100% faster | my O(n) solution
๐จโ๐ป Vazifa
Linked List-ni orqadan oldinga bo'lgan tartibga solish.
๐จโ๐ป Tahlil
Ko'rib turganingizdek, men rekursiya ishlatganman.
Yechimning asosiy g'oyasi - bu ko'rsatgichlarni shunday o'zgartirishki, ular keyingi emas, oldingi elementga ko'rsatishi kerak
Asosiy holat 1: element null-ga tengligini tekshirish
Asosiy holat 2: agar oxirgi elementga yetib kelsak, oxirgi element undan bitta oldin joylashgan elementga ko'rsatish va oxirgi elementni qaytarish.
Men
Oxirida shu harakatlarni rekursiya orqali takrorlash uchun hozirgi elementni oldingisini o'rniga va
Tushunarli bo'ldi degan umiddaman.
Omad!โ๏ธ๐ค
Hammaga salom. Keling shu masalaning yechimini ko'rib chiqamiz.
๐ข Oson daraja: 206. Reverse Linked List
๐จโ๐ป Mening yechimim: Java | 100% faster | my O(n) solution
๐จโ๐ป Vazifa
Linked List-ni orqadan oldinga bo'lgan tartibga solish.
๐จโ๐ป Tahlil
Ko'rib turganingizdek, men rekursiya ishlatganman.
Yechimning asosiy g'oyasi - bu ko'rsatgichlarni shunday o'zgartirishki, ular keyingi emas, oldingi elementga ko'rsatishi kerak
Asosiy holat 1: element null-ga tengligini tekshirish
Asosiy holat 2: agar oxirgi elementga yetib kelsak, oxirgi element undan bitta oldin joylashgan elementga ko'rsatish va oxirgi elementni qaytarish.
Men
tempNext o'zgaruvchisi orqali keyingi elementga ko'rsatgichni saqlab turaman va hozirgi elementning next ko'rsatgichinihead.next = prev orqali oldingisiga ko'rsatadigan qilib qo'yaman.Oxirida shu harakatlarni rekursiya orqali takrorlash uchun hozirgi elementni oldingisini o'rniga va
tempNext-ni hozirgini o'rniga qo'yib, parametrlar sifatida berib, funksiyani chaqiraman.Tushunarli bo'ldi degan umiddaman.
Omad!โ๏ธ๐ค
LeetCode
Reverse Linked List - LeetCode
Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
Example 1:
[https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg]
Input: head = [1,2,3,4โฆ
๐5๐1
๐ฌ๐ง 328. Odd Even Linked List
Hi, guys! Today we will solve a Linked List problem again.
Challenge for you: Can you solve it in O(N) time?
๐ก Medium: 328. Odd Even Linked List
๐จโ๐ป My solution: Java O(1) Memory | O(N) Time Solution
Good luck!โ๏ธ
Hi, guys! Today we will solve a Linked List problem again.
Challenge for you: Can you solve it in O(N) time?
๐ก Medium: 328. Odd Even Linked List
๐จโ๐ป My solution: Java O(1) Memory | O(N) Time Solution
Good luck!โ๏ธ
LeetCode
Odd Even Linked List - LeetCode
Can you solve this real interview question? Odd Even Linked List - Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is consideredโฆ
The first node is consideredโฆ
๐3
๐บ๐ฟ 328. Odd Even Linked List
Hammaga salom! Bugun ham Linked List-ga taalluqli masala yechamiz. Bu masalada sizlar list-dagi toq sonli indeks elementlarni, keyin esa juft indeksli elementlarni chiqarishingiz lozim.
Sizlarga challenge: O(N) tezlikda ishlaydigan algoritm yoza olasizlarmi?
๐ก O'rta daraja: 328. Odd Even Linked List
๐จโ๐ป Mening yechimim: Java O(1) Memory | O(N) Time Solution
Omad!โ๏ธ
Hammaga salom! Bugun ham Linked List-ga taalluqli masala yechamiz. Bu masalada sizlar list-dagi toq sonli indeks elementlarni, keyin esa juft indeksli elementlarni chiqarishingiz lozim.
Sizlarga challenge: O(N) tezlikda ishlaydigan algoritm yoza olasizlarmi?
๐ก O'rta daraja: 328. Odd Even Linked List
๐จโ๐ป Mening yechimim: Java O(1) Memory | O(N) Time Solution
Omad!โ๏ธ
LeetCode
Odd Even Linked List - LeetCode
Can you solve this real interview question? Odd Even Linked List - Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is consideredโฆ
The first node is consideredโฆ
๐8
๐ฌ๐ง 328. Odd Even Linked List
๐ก Medium: 328. Odd Even Linked List
๐จโ๐ป My solution: Java O(1) Memory | O(N) Time Solution
Hi, guys! Let's check the solution for this problem.
๐จโ๐ป Task
Briefly, your task is to group nodes of a Linked List in such a way that nodes with odd indexes will come first and then you should place evenly-indexed nodes.
๐จโ๐ป Explanation
- First three if statements are validations of dummy cases.
- I create two pointers: a pointer as a head for odd list and a pointer as a head for the even list.[
- Then I create two pointers for managing oddly and evenly indexed nodes. [
- Inside
Then I do
Then assign next even node to the current even node by doing
Finally, just return
Thus, we could write O(N) algorithm using only pointers and by manipulating them.
Thanks for reading!
Peace!โ๏ธ๐ค
๐ก Medium: 328. Odd Even Linked List
๐จโ๐ป My solution: Java O(1) Memory | O(N) Time Solution
Hi, guys! Let's check the solution for this problem.
๐จโ๐ป Task
Briefly, your task is to group nodes of a Linked List in such a way that nodes with odd indexes will come first and then you should place evenly-indexed nodes.
๐จโ๐ป Explanation
- First three if statements are validations of dummy cases.
- I create two pointers: a pointer as a head for odd list and a pointer as a head for the even list.[
oddHead and evenHead]- Then I create two pointers for managing oddly and evenly indexed nodes. [
oddCurrent and evenCurrent]- Inside
while loop I create tempEven pointer to keep a pointer to next even node.Then I do
oddCurrent.next = oddCurrent.next.next in order to assign next odd node to the current odd node. So, this is how we create the list made up of oddly indexed nodes.Then assign next even node to the current even node by doing
evenCurrent.next = tempEven. Then I do evenCurrent = evenCurrent.next to keep looping thru list. The same applies to oddCurrent = oddCurrent.next.if (oddCurrent.next == null) break; - this line is necessary because in the end I assign head of the even list as a next node for the last node of the odd list. If don't add this line, then we may run into NullPointerException (NPE), since while loop stops only when oddCurrent is null. It means that oddCurrent.next is going to throw that NPE.Finally, just return
oddHead.Thus, we could write O(N) algorithm using only pointers and by manipulating them.
Thanks for reading!
Peace!โ๏ธ๐ค
LeetCode
Odd Even Linked List - LeetCode
Can you solve this real interview question? Odd Even Linked List - Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is consideredโฆ
The first node is consideredโฆ
๐2
๐บ๐ฟ 328. Odd Even Linked List
๐ก O'rta daraja: 328. Odd Even Linked List
๐จโ๐ป Mening yechimim: Java O(1) Memory | O(N) Time Solution
๐จโ๐ป Vazifa
Qisqa qilib aytganda, bizning vazifamiz - bu toq indeksli elementlarni boshiga, juft indeksli elementlarni oxiriga [berilgan tartibda] qo'yishimiz lozim.
๐จโ๐ป Izoh
- Birinchi uchta
- Keyin men ikkita ko'rsatgich yarataman: birinchisi toq indeksli elementlarning boshini saqlash uchun va ikkinchisi juft indeksli elementlarning boshiga ko'rsatgich saqlash uchun. [
- Keyin esa
-
Bundan keyin hozirgi toq indeksli elementga undan keyingi toq indeksli elementni
Keyin
Oxirida esa
Shunday qilib, O(N) algoritmini ko'rsatgichlarni manipulyatsiya qilgan holda yozdik.
O'qiganingiz va obuna bo'lganingiz uchun rahmat!
Omad!โ๏ธ๐ค
๐ก O'rta daraja: 328. Odd Even Linked List
๐จโ๐ป Mening yechimim: Java O(1) Memory | O(N) Time Solution
๐จโ๐ป Vazifa
Qisqa qilib aytganda, bizning vazifamiz - bu toq indeksli elementlarni boshiga, juft indeksli elementlarni oxiriga [berilgan tartibda] qo'yishimiz lozim.
๐จโ๐ป Izoh
- Birinchi uchta
if qator - oddiy [tentak] holatlarni tekshirish uchun.- Keyin men ikkita ko'rsatgich yarataman: birinchisi toq indeksli elementlarning boshini saqlash uchun va ikkinchisi juft indeksli elementlarning boshiga ko'rsatgich saqlash uchun. [
oddHead and evenHead]- Keyin esa
while loop ichida list-ni aylanib chiqish uchun ishlatiladigan ikkita ko'rsatgich yarataman: aktual toq indeksli va juft indeksli elementlarga ko'rsatgichlar. [oddCurrent and evenCurrent]-
while loop ichida tempEven o'zgaruvchisini yaratib, keyingi juft indeksli elementga ko'rsatgichni saqlayman.Bundan keyin hozirgi toq indeksli elementga undan keyingi toq indeksli elementni
oddCurrent.next = oddCurrent.next.next orqali ulayman. Shu harakat orqali biz faqat toq indeksli elementlardan mavjud list-ni yaratamiz.Keyin
evenCurrent.next = tempEven orqali hozirgi juft indeksli elementga undan keyingi juft indeksli elementni ulayman.evenCurrent = evenCurrent.next qatori aktual juft indeksli elementni yangilab turadi.oddCurrent = oddCurrent.next qatori ham huddi maqsadda yozilgan.if (oddCurrent.next == null) break; - bu qator juda muhim. Agar bu qator bo'lmasa bizning kodimiz NullPointerException (NPE) chiqarishi mumkin. Nega deysizmi? Chunki while loop faqat oddCurrent null-ga teng bo'lganda to'xtaydi. Bu holatda oddCurrent.next NPE tashab yuboradi, chunki oddCurrent null-ga teng.Oxirida esa
oddHead qaytaramiz.Shunday qilib, O(N) algoritmini ko'rsatgichlarni manipulyatsiya qilgan holda yozdik.
O'qiganingiz va obuna bo'lganingiz uchun rahmat!
Omad!โ๏ธ๐ค
LeetCode
Odd Even Linked List - LeetCode
Can you solve this real interview question? Odd Even Linked List - Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is consideredโฆ
The first node is consideredโฆ
๐3
๐ฌ๐ง 86. Partition List
Hi, guys! Another day - another Linked List problem.
Challenge for you: Can you solve it in O(N) time?
๐ก Medium: 86. Partition List
๐จโ๐ป My solution: Java 100% Faster Solution | O(N)
Good luck!โ๏ธ
Hi, guys! Another day - another Linked List problem.
Challenge for you: Can you solve it in O(N) time?
๐ก Medium: 86. Partition List
๐จโ๐ป My solution: Java 100% Faster Solution | O(N)
Good luck!โ๏ธ
LeetCode
Partition List - LeetCode
Can you solve this real interview question? Partition List - Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of theโฆ
You should preserve the original relative order of theโฆ
๐4