LeetCodin
1.4K subscribers
160 photos
3 videos
2 files
303 links
LeetCode 1700+
๐Ÿ‡บ๐Ÿ‡ฟ๐Ÿ‡บ๐Ÿ‡ธ
Motto: ะŸะพะฑะตะดะฐ ะปัŽะฑะธั‚ ะฟะพะดะณะพั‚ะพะฒะบัƒ

- Getting ready for FAANG | FAANGga tayyorlanamiz
- Data Structures and Algorithms | Ma'lumotlar tuzilmalari va Algoritmlar
- SWE at Deloitte ๐Ÿ‡บ๐Ÿ‡ธ
- LeetCode: https://leetcode.com/tbekpro/
Download Telegram
๐Ÿ‡ฌ๐Ÿ‡ง 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!โœŒ๏ธ
๐Ÿ‘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!โœŒ๏ธ
๐Ÿ‘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 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! โœŒ๏ธ
๐Ÿณ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, 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! โœŒ๏ธ
๐Ÿ‘3๐Ÿฅฐ1
๐ŸŽ‰8๐Ÿ‘2โค1๐Ÿณ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!โœŒ๏ธ
๐Ÿ‘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! โœŒ๏ธ
๐Ÿ‘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!โœŒ๏ธ
๐Ÿ‘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! โœŒ๏ธ
๐Ÿ‘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! โœŒ๏ธ๐Ÿค“
๐Ÿ‘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! โœŒ๏ธ๐Ÿค“
๐Ÿ‘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 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!โœŒ๏ธ๐Ÿค“
๐Ÿ‘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 tempNext o'zgaruvchisi orqali keyingi elementga ko'rsatgichni saqlab turaman va hozirgi elementning next ko'rsatgichini
head.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!โœŒ๏ธ๐Ÿค“
๐Ÿ‘5๐Ÿ‘1
๐Ÿ”ฅ14๐Ÿ‘4
๐Ÿ‡ฌ๐Ÿ‡ง 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!โœŒ๏ธ
๐Ÿ‘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!โœŒ๏ธ
๐Ÿ‘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.[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!โœŒ๏ธ๐Ÿค“
๐Ÿ‘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 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!โœŒ๏ธ๐Ÿค“
๐Ÿ‘3
๐Ÿ‘8
๐Ÿ‡ฌ๐Ÿ‡ง 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!โœŒ๏ธ
๐Ÿ‘4