π¬π§ Theory. Linked LIst. delete().
Hi, guys! Let's check what should we write inside of delete() method of a simple Linked List.
1. We create two variables
- Suppose you have three nodes in a list and you need to remove the second node. What you do?
- You just need to take the first node and assign the third one as next node for it. Then the initial second node will no longer belong to the list.
2. Inside the while loop we loop until we find a node with given value.
- If next node is null then return null, since we didn't find a node with data equal to
- If next node exists, then we assign current node to
3.
- When while loop stops we check if the reference of
- If
Q: Why we compare references of
A: Because we assigned reference first to current in the beginning and if
That's it for today :)
Peace! βοΈπ₯Ά
Hi, guys! Let's check what should we write inside of delete() method of a simple Linked List.
public Node delete(int val) {
Node current = first;
Node previous = first;
while (current.data != val) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
π¨βπ» Explanation1. We create two variables
previous and current and assign the first node to them. The idea behind deleting is the following:- Suppose you have three nodes in a list and you need to remove the second node. What you do?
- You just need to take the first node and assign the third one as next node for it. Then the initial second node will no longer belong to the list.
2. Inside the while loop we loop until we find a node with given value.
- If next node is null then return null, since we didn't find a node with data equal to
val,- If next node exists, then we assign current node to
previous variable and make current point to the next node.3.
- When while loop stops we check if the reference of
current equals to the reference of first. If they are equal, then we just make the first point to next. It means that the first node's data is equal to val and we remove the initial first node from the list.- If
current is not equal to first then we just assign next of the current as the next for the previous [like I explained in the beginning].Q: Why we compare references of
current and first?A: Because we assigned reference first to current in the beginning and if
current == first is true, that means we didn't go inside of while loop and current still has the same value.That's it for today :)
Peace! βοΈπ₯Ά
π3
πΊπΏ Nazariya. Linked List. delete().
Hammaga salom! Keling bugun delete() [qidirilayotgan tugunni o'chirish] metodini oddiy Linked List-da qanday bo'lishini ko'rib chiqamiz.
1. Biz ikkita
- Tasavvur qiling sizda 3ta tugundan iborat list bor va siz o'rtadagi tugunni o'chirmoqchisiz. Buning uchun nima qilish lozim?
- Siz birinchi tugunning keyingi tugunga ko'rsatuvchi ko'rsatgichni o'rtadagi emas oxiridagi tugunga ko'rsatadigan qilasiz.
2. While loop orqali biz list-ni aylanib chiqamiz. Agar berilgan
- Agar keyingi tugun null bo'lsa biz null qaytaramiz, chunki biz kerakli tugunni topa olmadik.
- Agar keyingi tugun null bo'lmasa, biz
3.
- While loop to'xtaganida biz
- Agar
S: Nimaga biz
J: Chunki biz metodning boshida
Bugunga shu :)
Bunday sovuq havoda LeetCode-da masala yechish qiyin bo'lmayaptimi? π₯Άπ₯Άπ₯Ά
Omad! βοΈπ₯Ά
Hammaga salom! Keling bugun delete() [qidirilayotgan tugunni o'chirish] metodini oddiy Linked List-da qanday bo'lishini ko'rib chiqamiz.
public Node delete(int val) {
Node current = first;
Node previous = first;
while (current.data != val) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
π¨βπ» Izoh1. Biz ikkita
previous va current o'zgaruvchilarini yaratamiz va ikkalasiga birinchi elementga ko'rsatgichni qiymatini tayinlaymiz. Tugun o'chirish tagidagi mantiq quyidagicha:- Tasavvur qiling sizda 3ta tugundan iborat list bor va siz o'rtadagi tugunni o'chirmoqchisiz. Buning uchun nima qilish lozim?
- Siz birinchi tugunning keyingi tugunga ko'rsatuvchi ko'rsatgichni o'rtadagi emas oxiridagi tugunga ko'rsatadigan qilasiz.
2. While loop orqali biz list-ni aylanib chiqamiz. Agar berilgan
val tugun ichidagi ma'lumotiga teng bo'lsa, to'xtaymiz.- Agar keyingi tugun null bo'lsa biz null qaytaramiz, chunki biz kerakli tugunni topa olmadik.
- Agar keyingi tugun null bo'lmasa, biz
previous o'zgaruvchisiga hozirgi (current) tugunini tayinlaymiz va hozirgi (current) tugunga undan keyingi tugunni tayinlaymiz.3.
- While loop to'xtaganida biz
current va first ko'rsatgichlarini tengligini tekshiramiz. Agar teng bo'lishsa, biz first o'zgaruvchisiga undan keyingi tugunni tayinlaymiz. Bu harakat orqali biz birinchi tugunni o'chiramiz, chunki, uning data qiymati val-ga teng.- Agar
current first-ga teng bo'lmasa biz current-ning keyingi tugunini previous-ning keyingi tuguni sifatida tayinlaymiz [boshida aytganimdek].S: Nimaga biz
current va first ko'rsatgichlarini "==" operatori orqali tekshiramiz?J: Chunki biz metodning boshida
previous va current-larga first ko'rsatgichini tayinladik va current == first true bo'lsa, demak while loop-ning ichiga kirmadik va current hali ham shu qiymatni saqlab turibdi.Bugunga shu :)
Bunday sovuq havoda LeetCode-da masala yechish qiyin bo'lmayaptimi? π₯Άπ₯Άπ₯Ά
Omad! βοΈπ₯Ά
π8
π¬π§ Linked List. Finish.
Hi, guys! I'd like to inform that we are done with simple unidirected Linked List topic! Yaay! Pat yourself on the back :)
Here are the links to all posts related to this topic. Enjoy!
1. Theory. Linked List. Intro.
2. Theory. Linked List. Relationship, Not Position.
3. Theory. Linked List. Time complexity.
4. Theory. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Theory. Linked List. find().
7. Theory. Linked LIst. delete().
Bonus: Link-1 and Link-2.
Let me know if these posts were useful by giving some reactions to the post.
Thanks! π¨βπ»π€π€π¨βπ»
Hi, guys! I'd like to inform that we are done with simple unidirected Linked List topic! Yaay! Pat yourself on the back :)
Here are the links to all posts related to this topic. Enjoy!
1. Theory. Linked List. Intro.
2. Theory. Linked List. Relationship, Not Position.
3. Theory. Linked List. Time complexity.
4. Theory. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Theory. Linked List. find().
7. Theory. Linked LIst. delete().
Bonus: Link-1 and Link-2.
Let me know if these posts were useful by giving some reactions to the post.
Thanks! π¨βπ»π€π€π¨βπ»
Telegram
LeetCodin
π¬π§ Theory. Linked List.
Hi, guys! Let's start series of posts where I'll try to cover Linked List data structure. I'm going to discuss unidirectional linked list. Don't forget that I use Java and some explanations may be language specific.
In linked listβ¦
Hi, guys! Let's start series of posts where I'll try to cover Linked List data structure. I'm going to discuss unidirectional linked list. Don't forget that I use Java and some explanations may be language specific.
In linked listβ¦
π4β€1
πΊπΏ Linked List. Finish.
Hammaga salom! Biz bir tomonlama yo'naltirilgan Linked List (ro'yxat) mavzusini tugatdik! Urraaa! O'zingizga shirinlik olib, o'zingizni tabriklab qo'ying :)
Pastda bu mavzu bo'yicha yozgan postlarga havolalarni qoldiraman.
1. Nazariya. Linked List. Intro.
2. Nazariya. Linked List. Pozitsiya (joy) emas, munosabat
3. Nazariya. Linked List. Vaqt murakkabligi [Time complexity].
4. Nazariya. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Nazariya. Linked List. find().
7. Nazariya. Linked LIst. delete().
Bonus: Havola-1 and Havola-2.
Agar shu postlar sizlarga foydali bo'lgan bo'lsa, bu postga reaction-lar qo'ying. Menga bu motivatsiya beradi.
Rahmat! π¨βπ»π€π€π¨βπ»
Hammaga salom! Biz bir tomonlama yo'naltirilgan Linked List (ro'yxat) mavzusini tugatdik! Urraaa! O'zingizga shirinlik olib, o'zingizni tabriklab qo'ying :)
Pastda bu mavzu bo'yicha yozgan postlarga havolalarni qoldiraman.
1. Nazariya. Linked List. Intro.
2. Nazariya. Linked List. Pozitsiya (joy) emas, munosabat
3. Nazariya. Linked List. Vaqt murakkabligi [Time complexity].
4. Nazariya. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Nazariya. Linked List. find().
7. Nazariya. Linked LIst. delete().
Bonus: Havola-1 and Havola-2.
Agar shu postlar sizlarga foydali bo'lgan bo'lsa, bu postga reaction-lar qo'ying. Menga bu motivatsiya beradi.
Rahmat! π¨βπ»π€π€π¨βπ»
Telegram
LeetCodin
πΊπΏ Nazariya. Linked List.
Hammaga salom! Keling shu Linked List mavzusini yaxshilab o'rganaylik. Bu mavzuni shoshmasdan bir nechta postlarda ko'rib chiqamiz. Men bir tomon yo'naltirilgan list haqida yozmoqchiman.
NB:
1. List - bu ro'yxat. Lekin list debβ¦
Hammaga salom! Keling shu Linked List mavzusini yaxshilab o'rganaylik. Bu mavzuni shoshmasdan bir nechta postlarda ko'rib chiqamiz. Men bir tomon yo'naltirilgan list haqida yozmoqchiman.
NB:
1. List - bu ro'yxat. Lekin list debβ¦
π₯17π3π2π¨βπ»1
π¬π§ Practice. 404. Sum of Left Leaves.
Hi, guys! Let's have some practice after theoretical posts. Here is a binary tree problem for you.
π’ Easy: 404. Sum of Left Leaves
π¨βπ» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Let me know how much time does it take to solve this problem.
Good luck! βοΈπ€
Hi, guys! Let's have some practice after theoretical posts. Here is a binary tree problem for you.
π’ Easy: 404. Sum of Left Leaves
π¨βπ» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Let me know how much time does it take to solve this problem.
Good luck! βοΈπ€
LeetCode
Sum of Left Leaves - LeetCode
Can you solve this real interview question? Sum of Left Leaves - Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
π1
πΊπΏ Amaliyot. 404. Sum of Left Leaves.
Hammaga salom! Keling amaliyot ham bajaraylik. Bugun sizlarga binar daraxt mavzusiga oid masala tayyorlab qo'ydim.
π’ Oson: 404. Sum of Left Leaves
π¨βπ» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Comment-larda bu masalani yechishga qancha vaqt sarflaganingizni yozing.
Omad! βοΈπ€
Hammaga salom! Keling amaliyot ham bajaraylik. Bugun sizlarga binar daraxt mavzusiga oid masala tayyorlab qo'ydim.
π’ Oson: 404. Sum of Left Leaves
π¨βπ» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Comment-larda bu masalani yechishga qancha vaqt sarflaganingizni yozing.
Omad! βοΈπ€
LeetCode
Sum of Left Leaves - LeetCode
Can you solve this real interview question? Sum of Left Leaves - Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
π3
π¬π§ Practice. 404. Sum of Left Leaves.
π’ Easy: 404. Sum of Left Leaves
π¨βπ» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Hi, guys! Let's see one of the possible solutions for the problem above.
π¨βπ» Task
Your task is to return sum of all left leaves.
π¨βπ» Solution
The solution will be easy for you if you read my post here. This is basic DFS tree problem.
- Since we are using recursive approach I check the base case: if
- I create a sum variable and make its value 0.
- Then I check a condition I need. Our condition for a left leaf is when left node is not null and left and right nodes of it are null. If it's true, we just add that to
- Finally, I recursively return sum for each subtree by recursively calling the method for left and right nodes.
This is it :)
Good luck! βοΈπ€
π’ Easy: 404. Sum of Left Leaves
π¨βπ» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Hi, guys! Let's see one of the possible solutions for the problem above.
π¨βπ» Task
Your task is to return sum of all left leaves.
π¨βπ» Solution
The solution will be easy for you if you read my post here. This is basic DFS tree problem.
- Since we are using recursive approach I check the base case: if
node is null then return 0.- I create a sum variable and make its value 0.
- Then I check a condition I need. Our condition for a left leaf is when left node is not null and left and right nodes of it are null. If it's true, we just add that to
sum.- Finally, I recursively return sum for each subtree by recursively calling the method for left and right nodes.
This is it :)
Good luck! βοΈπ€
LeetCode
Sum of Left Leaves - LeetCode
Can you solve this real interview question? Sum of Left Leaves - Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
β€1
πΊπΏ Amaliyot. 404. Sum of Left Leaves.
π’ Oson: 404. Sum of Left Leaves
π¨βπ» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Hammaga salom! Keling shu masalaning yechimlaridan bittasini ko'rib chiqaylik.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan binar daraxtning chap barglarining jamini topish. Binar daraxtda barg - bu bollari yo'q tugundir.
π¨βπ» Yechim
Yechimni yaxshiroq tushunishingiz uchun eski postimga havola qoldiraman - Binar daraxtni aylanib chiqish.
- Rekursiv usul ishlatayotganimiz uchun asosiy holatni tekshiramiz:
- Keyin esa bizga kerak bo'lgan shartni tekshiramiz. Tugun barg bo'lishi uchun uning o'zi
- Oxirida, shu sum va bola daraxtlardan olingan natijalarni qo'shamiz. Shu natijalarni rekursiv chaqiruv orqali olamiz.
Bugunga shu :)
Omad! βοΈπ€
π’ Oson: 404. Sum of Left Leaves
π¨βπ» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution
Hammaga salom! Keling shu masalaning yechimlaridan bittasini ko'rib chiqaylik.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan binar daraxtning chap barglarining jamini topish. Binar daraxtda barg - bu bollari yo'q tugundir.
π¨βπ» Yechim
Yechimni yaxshiroq tushunishingiz uchun eski postimga havola qoldiraman - Binar daraxtni aylanib chiqish.
- Rekursiv usul ishlatayotganimiz uchun asosiy holatni tekshiramiz:
if (node == null) return 0;
- Sum o'zgaruvchisini yarataman va qiymatini 0 qilaman.- Keyin esa bizga kerak bo'lgan shartni tekshiramiz. Tugun barg bo'lishi uchun uning o'zi
null-ga teng emasligi va uning bollari null-ga tengligini tekshiramiz. Agar shart true bo'lsa, sum shu bargning qiymatiga teng bo'ladi.- Oxirida, shu sum va bola daraxtlardan olingan natijalarni qo'shamiz. Shu natijalarni rekursiv chaqiruv orqali olamiz.
Bugunga shu :)
Omad! βοΈπ€
LeetCode
Sum of Left Leaves - LeetCode
Can you solve this real interview question? Sum of Left Leaves - Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
[https:β¦
β€4
π¬π§ 1009. Complement of Base 10 Integer
Hi, guys! Let's solve one interesting problem.
π’ Easy: 1009. Complement of Base 10 Integer
π¨βπ» My solution: Java O(1) | 1 line Solution
One challenge for you: Can you write 1 line solution for this?
Good luck! βοΈπ€
Hi, guys! Let's solve one interesting problem.
π’ Easy: 1009. Complement of Base 10 Integer
π¨βπ» My solution: Java O(1) | 1 line Solution
One challenge for you: Can you write 1 line solution for this?
Good luck! βοΈπ€
LeetCode
Complement of Base 10 Integer - LeetCode
Can you solve this real interview question? Complement of Base 10 Integer - The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
* For example, The integer 5 is "101" inβ¦
* For example, The integer 5 is "101" inβ¦
π1
πΊπΏ 1009. Complement of Base 10 Integer
Hammaga salom! Keling bugun bitta qiziqarli masala yechamiz.
π’ Oson: 1009. Complement of Base 10 Integer
π¨βπ» Mening yechimim: Java O(1) | 1 line Solution
Sizlarga challenge: bir qatorli yechim topa olasizmi?
Omad! βοΈπ€
Hammaga salom! Keling bugun bitta qiziqarli masala yechamiz.
π’ Oson: 1009. Complement of Base 10 Integer
π¨βπ» Mening yechimim: Java O(1) | 1 line Solution
Sizlarga challenge: bir qatorli yechim topa olasizmi?
Omad! βοΈπ€
LeetCode
Complement of Base 10 Integer - LeetCode
Can you solve this real interview question? Complement of Base 10 Integer - The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
* For example, The integer 5 is "101" inβ¦
* For example, The integer 5 is "101" inβ¦
π5
π¬π§ 1009. Complement of Base 10 Integer
π’ Easy: 1009. Complement of Base 10 Integer
π¨βπ» My solution: Java O(1) | 1 line Solution
Let's check the solution for the problem above.
π¨βπ» Task
Your task is to flip all 0's to 1's and all 1's to 0's of binary representation of given number and return new integer based on this.
π¨βπ» Solution
My idea is to execute XOR operation to the binary representation of given number and binary representation of other number with the same length filled with only 1's.
For example, we are given 5. Its binary representation is "101". I apply XOR to "101" and "111". We get "010" which is 2 in base-10.
The steps are:
Then I substract 1. 8 - 1 = 7. 7 in binary is "111".
Finally, I apply XOR to
So, this is it :)
Good luck! βοΈπ€
π’ Easy: 1009. Complement of Base 10 Integer
π¨βπ» My solution: Java O(1) | 1 line Solution
Let's check the solution for the problem above.
π¨βπ» Task
Your task is to flip all 0's to 1's and all 1's to 0's of binary representation of given number and return new integer based on this.
π¨βπ» Solution
My idea is to execute XOR operation to the binary representation of given number and binary representation of other number with the same length filled with only 1's.
For example, we are given 5. Its binary representation is "101". I apply XOR to "101" and "111". We get "010" which is 2 in base-10.
The steps are:
Integer.toBinaryString(n).length() - Find binary representation and its length. In the example above it is 3.(1 << Integer.toBinaryString(n).length())will look like
1 << 3. By left shift I move 1 three digits to left and will get "1000". In base-10 representation this is 8.Then I substract 1. 8 - 1 = 7. 7 in binary is "111".
Finally, I apply XOR to
n and 7. So, this is it :)
Good luck! βοΈπ€
LeetCode
Complement of Base 10 Integer - LeetCode
Can you solve this real interview question? Complement of Base 10 Integer - The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
* For example, The integer 5 is "101" inβ¦
* For example, The integer 5 is "101" inβ¦
π1
πΊπΏ 1009. Complement of Base 10 Integer
π’ Oson: 1009. Complement of Base 10 Integer
π¨βπ» Mening yechimim: Java O(1) | 1 line Solution
Hammaga salom! Keling kechagi masalaning yechimini ko'rib chiqaylik.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan sonning binar ko'rinishidagi hamma 0-larni 1-lar bilan va 1-larni 0-lar bilan almashtirib chiqish. Va natijadagi hosil bo'lgan sonni qaytarish.
π¨βπ» Yechim
Yechimning tagidagi g'oya qiyin emas. Men berilgan sonning binar ko'rinishi uzunligini topaman va shu uzunlikdagi faqat 1-lardan iborat binar sonni yarataman. Keyin shu ikkita son ustida XOR operatsiyasini bajaraman.
Masalan, bizga 5 berilgan, "101" esa uning binar ko'rinishi. Shu uzunlikka ega "111" binar sonini yaratamiz va shu ikkita son ustida XOR-ni bajaramiz. Natijada "010" binarli son kelib chiqadi va base-10 ko'rinishida bu 2-ga teng.
Harakatlarimiz quyidagicha:
Keyin 8-dan 1-ni ayriman. 7-ning binar ko'rinishi "111" bo'ladi.
Oxirida
Shu xolos :)
Omad! βοΈπ€
π’ Oson: 1009. Complement of Base 10 Integer
π¨βπ» Mening yechimim: Java O(1) | 1 line Solution
Hammaga salom! Keling kechagi masalaning yechimini ko'rib chiqaylik.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan sonning binar ko'rinishidagi hamma 0-larni 1-lar bilan va 1-larni 0-lar bilan almashtirib chiqish. Va natijadagi hosil bo'lgan sonni qaytarish.
π¨βπ» Yechim
Yechimning tagidagi g'oya qiyin emas. Men berilgan sonning binar ko'rinishi uzunligini topaman va shu uzunlikdagi faqat 1-lardan iborat binar sonni yarataman. Keyin shu ikkita son ustida XOR operatsiyasini bajaraman.
Masalan, bizga 5 berilgan, "101" esa uning binar ko'rinishi. Shu uzunlikka ega "111" binar sonini yaratamiz va shu ikkita son ustida XOR-ni bajaramiz. Natijada "010" binarli son kelib chiqadi va base-10 ko'rinishida bu 2-ga teng.
Harakatlarimiz quyidagicha:
Integer.toBinaryString(n).length() - berilgan sonning binar ko'rinishini topib, uning uzunligini aniqlaymiz.(1 << Integer.toBinaryString(n).length())ifodasi
1 << 3 ko'rinishga ega bo'ladi. Bu operatsiya [left shift] orqali 1-ni uchta bitga chapga suramiz va "1000" hosil bo'ladi. Bu sonning base-10 ko'rinishi 8-ga teng.Keyin 8-dan 1-ni ayriman. 7-ning binar ko'rinishi "111" bo'ladi.
Oxirida
n va 7 sonlari ustida XOR-ni bajaraman.Shu xolos :)
Omad! βοΈπ€
LeetCode
Complement of Base 10 Integer - LeetCode
Can you solve this real interview question? Complement of Base 10 Integer - The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
* For example, The integer 5 is "101" inβ¦
* For example, The integer 5 is "101" inβ¦
π4
π¬π§ Theory. Stack.
Hi, guys! Since we discussed Linked Lists, I think the logical continuation of the topic should be such Data Structures as Stack and Queue. So, let's start from Stack.
Stack is a data structure which allows access to only one data node - the last node inserted. When you remove [or pop] that item, then you can access next item and etc.
The approach implemented by this DS is called Last-In-First-Out or LIFO. It means that newest element added will be processed first.
We can write our own Stack. We will need two classes for this:
1. Node class. This is the same Node class that we utilized for Linked List - link. Basically, we build a linked list for Stack, but utilize it in a specific manner. We will discuss this later.
2. Stack class. Stack class contains a link to the first node. If it's null, then we don't have node in our Stack yet. Stack also has:
- constructor(s) to create our Stack with certain state.
- methods: adding push(), removing pop(), displaying last element peek(), and etc.
That's it for today.
Sources:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Good luck! βοΈπ€
Hi, guys! Since we discussed Linked Lists, I think the logical continuation of the topic should be such Data Structures as Stack and Queue. So, let's start from Stack.
Stack is a data structure which allows access to only one data node - the last node inserted. When you remove [or pop] that item, then you can access next item and etc.
The approach implemented by this DS is called Last-In-First-Out or LIFO. It means that newest element added will be processed first.
We can write our own Stack. We will need two classes for this:
1. Node class. This is the same Node class that we utilized for Linked List - link. Basically, we build a linked list for Stack, but utilize it in a specific manner. We will discuss this later.
2. Stack class. Stack class contains a link to the first node. If it's null, then we don't have node in our Stack yet. Stack also has:
- constructor(s) to create our Stack with certain state.
- methods: adding push(), removing pop(), displaying last element peek(), and etc.
That's it for today.
Sources:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Good luck! βοΈπ€
Telegram
LeetCodin
π¬π§ Linked List. Finish.
Hi, guys! I'd like to inform that we are done with simple unidirected Linked List topic! Yaay! Pat yourself on the back :)
Here are the links to all posts related to this topic. Enjoy!
1. Theory. Linked List. Intro.
2. Theory. Linkedβ¦
Hi, guys! I'd like to inform that we are done with simple unidirected Linked List topic! Yaay! Pat yourself on the back :)
Here are the links to all posts related to this topic. Enjoy!
1. Theory. Linked List. Intro.
2. Theory. Linkedβ¦
π6
πΊπΏ Nazariya. Stack.
Hammaga salom! Sizlar bilan Linked List mavzusini ko'rib chiqdik. Keling endi shu mavzuning mantiqiy davomi - shu ma'lumot tuzilmasi [MT] asosida qurilgan Stack va Queue MT-larini ko'rib chiqamiz. Stack-dan boshlaymiz.
Stack - bu faqat bitta, oxirgi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [pop()] so'ng, keyingi element bilan foydalana olasiz.
Ushbu MT tomonidan qo'llaniladigan usul Last-In-First-out yoki LIFO deb ataladi. Bu degani MT-ga oxirgi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.
Biz ham o'zimizning sodda Stack MT-mizni yozishimiz mumkin. Bizga ikkita klass kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Stack linked list ustida qurilgan desak bo'ladi, lekin buni keyinroq ko'rib chiqamiz.
2. Stack klassi. Stack klassi o'zida oxirgi tugunga ko'rsatuvchi ko'rsatgichni saqlaydi. Agar u null bo'lsa, bizning Stack'imiz bo'sh degani. Stack-da yana:
- ma'lum bir holat bilan Stack-ni yaratuvchi konstruktor;
- qo'shish push(), olib tashlash pop(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.
Bugunga shu.
Manbalar:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Hammaga omad! βοΈπ€
Hammaga salom! Sizlar bilan Linked List mavzusini ko'rib chiqdik. Keling endi shu mavzuning mantiqiy davomi - shu ma'lumot tuzilmasi [MT] asosida qurilgan Stack va Queue MT-larini ko'rib chiqamiz. Stack-dan boshlaymiz.
Stack - bu faqat bitta, oxirgi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [pop()] so'ng, keyingi element bilan foydalana olasiz.
Ushbu MT tomonidan qo'llaniladigan usul Last-In-First-out yoki LIFO deb ataladi. Bu degani MT-ga oxirgi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.
Biz ham o'zimizning sodda Stack MT-mizni yozishimiz mumkin. Bizga ikkita klass kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Stack linked list ustida qurilgan desak bo'ladi, lekin buni keyinroq ko'rib chiqamiz.
2. Stack klassi. Stack klassi o'zida oxirgi tugunga ko'rsatuvchi ko'rsatgichni saqlaydi. Agar u null bo'lsa, bizning Stack'imiz bo'sh degani. Stack-da yana:
- ma'lum bir holat bilan Stack-ni yaratuvchi konstruktor;
- qo'shish push(), olib tashlash pop(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.
Bugunga shu.
Manbalar:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Hammaga omad! βοΈπ€
Telegram
LeetCodin
πΊπΏ Linked List. Finish.
Hammaga salom! Biz bir tomonlama yo'naltirilgan Linked List (ro'yxat) mavzusini tugatdik! Urraaa! O'zingizga shirinlik olib, o'zingizni tabriklab qo'ying :)
Pastda bu mavzu bo'yicha yozgan postlarga havolalarni qoldiraman.
1. Nazariya.β¦
Hammaga salom! Biz bir tomonlama yo'naltirilgan Linked List (ro'yxat) mavzusini tugatdik! Urraaa! O'zingizga shirinlik olib, o'zingizni tabriklab qo'ying :)
Pastda bu mavzu bo'yicha yozgan postlarga havolalarni qoldiraman.
1. Nazariya.β¦
π12
π¬π§ Theory. Stack methods.
Hi, guys! Yesterday we discussed what Stack Data Structure is. Let's dig a little big deeper.
push():
- Adds new element to your stack. Here we can use Linked List's insertFirst() method.
pop():
- This method removes last element and returns it. Here we can use Linked List's deleteFirst() method.
peek():
- This method returns last element without deleting it. Here we just return the first element of our Linked List.
You can see in the picture that we have built our Stack based on Linked List. π
That's it.
P.S. LeetCode is challenging us with backtracking problems. It's getting tough, honestly.
Have a good day!βοΈπ€
Hi, guys! Yesterday we discussed what Stack Data Structure is. Let's dig a little big deeper.
push():
- Adds new element to your stack. Here we can use Linked List's insertFirst() method.
pop():
- This method removes last element and returns it. Here we can use Linked List's deleteFirst() method.
peek():
- This method returns last element without deleting it. Here we just return the first element of our Linked List.
You can see in the picture that we have built our Stack based on Linked List. π
That's it.
P.S. LeetCode is challenging us with backtracking problems. It's getting tough, honestly.
Have a good day!βοΈπ€
Telegram
LeetCodin
π¬π§ Linked List. insertFirst() and deleteFirst().
Hi, guys! Let's see how we write methods above for a simple Linked List in Java.
π insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);
newFirstNode.nextβ¦
Hi, guys! Let's see how we write methods above for a simple Linked List in Java.
π insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);
newFirstNode.nextβ¦
π3
πΊπΏ Nazariya. Stack-ning metodlari.
Hammaga salom! Keling kechagi Stack mavzusini sal chuqurroq ko'rib chiqamiz.
push():
- Stack-ga yangi element qo'shamiz. Bu yerda Linked List-ning insertFirst() metodini ishlatsak bo'ladi.
pop():
- Stack-ning oxirgi elementini olib tashlaydi va uni qaytaradi. Bu yerda Linked List-ning deleteFirst() metodi bilan foydalansak bo'ladi.
peek():
- Stack-ning oxirgi elementini qaytaradi (u joyida qolaveradi). Bu yerda esa biz Linked List-ning birinchi elemtini qaytaramiz.
Postning tagida rasmda siz Linked List ustida qurilgan Stack-ni kodini ko'rishingiz mumkin.
Bugunga shu.
P.S. LeetCode backtracking masalalari bilan juda ham qiynayapti.
Yaxshi dam olingizlar! βοΈπ€
Hammaga salom! Keling kechagi Stack mavzusini sal chuqurroq ko'rib chiqamiz.
push():
- Stack-ga yangi element qo'shamiz. Bu yerda Linked List-ning insertFirst() metodini ishlatsak bo'ladi.
pop():
- Stack-ning oxirgi elementini olib tashlaydi va uni qaytaradi. Bu yerda Linked List-ning deleteFirst() metodi bilan foydalansak bo'ladi.
peek():
- Stack-ning oxirgi elementini qaytaradi (u joyida qolaveradi). Bu yerda esa biz Linked List-ning birinchi elemtini qaytaramiz.
Postning tagida rasmda siz Linked List ustida qurilgan Stack-ni kodini ko'rishingiz mumkin.
Bugunga shu.
P.S. LeetCode backtracking masalalari bilan juda ham qiynayapti.
Yaxshi dam olingizlar! βοΈπ€
Telegram
LeetCodin
πΊπΏ Linked List. insertFirst() va deleteFirst().
Hammaga salom! Keling bugun sarlavhada berilgan metodlarni Javada oddiy Linked List uchun ko'rib chiqaylik.
π insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);β¦
Hammaga salom! Keling bugun sarlavhada berilgan metodlarni Javada oddiy Linked List uchun ko'rib chiqaylik.
π insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);β¦
π4
π¬π§ Stack. Wrap up.
Hi, guys! So, we've looked through Stack topic. Let's do some practice with Stack Data Structure.
π’ Easy: 20. Valid Parentheses
π¨βπ» My solution: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Actually, there is much more elegant solution than mine. But! Right now it's not that important. Your task is to use Stack to solve the problem.
Good luck! βοΈπ€
Hi, guys! So, we've looked through Stack topic. Let's do some practice with Stack Data Structure.
π’ Easy: 20. Valid Parentheses
π¨βπ» My solution: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Actually, there is much more elegant solution than mine. But! Right now it's not that important. Your task is to use Stack to solve the problem.
Good luck! βοΈπ€
LeetCode
Valid Parentheses - LeetCode
Can you solve this real interview question? Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by theβ¦
An input string is valid if:
1. Open brackets must be closed by theβ¦
π4