🔁 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝗥𝗲𝘃𝗲𝗿𝘀𝗮𝗹 — 𝟯 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀 (𝗣𝘆𝘁𝗵𝗼𝗻) Reversing a linked list is one of the most common DSA interview questions — but the real value comes from understanding how different approaches impact performance. I implemented three approaches to reverse a singly linked list: 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 (𝗨𝘀𝗶𝗻𝗴 𝗔𝗿𝗿𝗮𝘆) Store values in a list and reassign ⏱ Time: O(n) 🧠 Space: O(n) 𝗦𝘁𝗮𝗰𝗸 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Push nodes into a stack and reconnect ⏱ Time: O(n) 🧠 Space: O(n) 𝗢𝗽𝘁𝗶𝗺𝗮𝗹 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 (𝗜𝗻-𝗽𝗹𝗮𝗰𝗲) 🚀 Reverse pointers without extra memory ⏱ Time: O(n) 🧠 Space: O(1) 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 💡 All approaches take the same time, but reducing space complexity from O(n) → O(1) makes the optimal solution industry-preferred. 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 📌 Before → 10 → 20 → 30 After → 30 → 20 → 10 𝗧𝗲𝗰𝗵 𝗨𝘀𝗲𝗱 🔧 Python I’m currently exploring Data Structures, System Design, and scalable architectures. 📁 Code includes clean structure + documentation + complexity analysis https://lnkd.in/dTkpkwhY Would love feedback or suggestions from the community 🙌 #Python #DataStructures #LinkedList #DSA #CodingInterview #SoftwareDevelopment #Learning #GitHub
Reversing a Linked List: 3 Approaches and Performance Analysis
More Relevant Posts
-
🚀 Solved Two Sum Problem with Optimal Approach | LeetCode Today I solved the classic Two Sum problem and focused on writing an efficient solution rather than just making it work. 💡 Problem: Given an array of integers, return indices of two numbers such that they add up to a target. ⚡ Approach: Instead of using the brute force method, I used a HashMap (dictionary) to store elements and their indices. 👉 Logic: Traverse the array once For each element, calculate: difference = target - current value Check if difference already exists in the HashMap If yes → return indices instantly 🔥 Time & Space Complexity: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🚀 Optimization: Improved from brute force O(n²) → O(n) using HashMap lookup 🏆 Result: ✔️ Accepted (All test cases passed) ✔️ Runtime: 0 ms (Beats 100%) 📌 Key Learnings: HashMap enables constant time lookup Thinking in terms of complement simplifies problems Optimization is key in coding interviews 💻 Tech Stack: Python | Data Structures & Algorithms 📈 Consistency + Practice = Growth 🚀 #leetcode #dsa #python #algorithms #coding #programming #softwareengineering #100DaysOfCode #tech
To view or add a comment, sign in
-
-
Merge In Between Linked Lists — and got it Accepted ✅ This problem really tested my understanding of: 🔹 Linked List traversal 🔹 Pointer manipulation 🔹 Edge case handling One small mistake in pointer connection... and everything breaks. 😅 But that’s where real learning happens. 💡 Key takeaway: In linked lists, it’s not about values—it's about how you connect nodes. Step by step, I’m getting stronger in data structures & algorithms and building the problem-solving mindset needed for top tech roles. 🔥 Consistency is the real game changer. #LeetCode #DSA #ProblemSolving #Python #CodingJourney #SoftwareDeveloper #FullStackDeveloper #KeepLearning
To view or add a comment, sign in
-
-
🚀 Day 344 of solving 365 medium questions on LeetCode! 🔥 Today’s challenge: “89. Gray Code” ✅ Problem: You are given an integer n. Your goal is to generate an n-bit Gray code sequence, which is an array of 2^n integers where every adjacent pair of numbers (including the first and last numbers) differs by exactly one single bit in their binary representation. ✅ Approach (Bit Manipulation / The Formula) You could solve this using backtracking or mirroring, but there is a mathematical cheat code that solves it instantly! Find the Size: First, we need to know exactly how many numbers to generate. For an n-bit sequence, there are exactly 2^n numbers. I used a bitwise left shift (1 << n) to calculate this size instantly. The Magic Formula: The i-th number in a standard Gray code sequence can always be found using the exact formula: i ^ (i >> 1). This takes the number, shifts its bits to the right by one, and applies a bitwise XOR against the original number. List Comprehension: I packed this entire logic into a single Python list comprehension that loops from 0 up to our calculated size. It applies the magic formula to every index i, generating the perfect sequence in one go! ✅ Key Insight Bitwise operations are essentially black magic when you know the right formulas. Recognizing that Gray code has a direct integer-to-sequence mapping completely eliminates the need for messy recursive state-tracking. What looks like a complex combinatorial sequence problem is actually just a one-line math trick! ✅ Complexity Time: O(2^n) — We must iterate to generate exactly 2^n elements for the sequence. Space: O(1) — Excluding the space required for the output array, the mathematical generation uses strictly constant auxiliary memory. 🔍 Python solution attached! 🔥 Flexing my coding skills until recruiters notice! #LeetCode365 #BitManipulation #Math #Python #ProblemSolving #DSA #Coding #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 𝗕𝘂𝗶𝗹𝘁 𝗮 𝗖𝗹𝗲𝗮𝗻 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 𝗼𝗳 𝗟𝗼𝗻𝗴𝗲𝘀𝘁 𝗖𝗼𝗺𝗺𝗼𝗻 𝗣𝗿𝗲𝗳𝗶𝘅 (𝗟𝗖𝗣) 𝗶𝗻 𝗣𝘆𝘁𝗵𝗼𝗻 As part of improving my problem-solving skills, I explored multiple approaches to solve the Longest Common Prefix problem. 🔍 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a list of strings, find the longest common prefix among them. 💡 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀 𝗜 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗲𝗱: 🔴 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 (𝗩𝗲𝗿𝘁𝗶𝗰𝗮𝗹 𝗦𝗰𝗮𝗻𝗻𝗶𝗻𝗴) Compare characters column-wise across all strings ⏱ Time: O(N × M) 🟡 𝗛𝗼𝗿𝗶𝘇𝗼𝗻𝘁𝗮𝗹 𝗦𝗰𝗮𝗻𝗻𝗶𝗻𝗴 Start with first string and shrink prefix step by step ⏱ Time: O(N × M) 🟢 𝗢𝗽𝘁𝗶𝗺𝗮𝗹 (𝗦𝗼𝗿𝘁𝗶𝗻𝗴 𝗧𝗿𝗶𝗰𝗸) Sort strings and compare only first & last ⏱ Time: O(N log N + M) ⚡ 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴𝘀: Early exit conditions improve performance significantly Choosing the right approach depends on input size and use case Clean code > complex code 🧠 𝗥𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲𝘀: Search suggestions (Autocomplete) Text processing systems Data normalization 📂 𝗖𝗼𝗱𝗲 𝗮𝘃𝗮𝗶𝗹𝗮𝗯𝗹𝗲 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: 👉 https://lnkd.in/gJQ-KgRP #Python #DataStructures #Algorithms #Coding #SoftwareDevelopment #ProblemSolving #Developers #GitHub #LearningJourney
To view or add a comment, sign in
-
-
I wasted HOURS writing bad Python code when I started in Data Analysis… These simple tricks changed everything 👇 🔹 List Comprehensions Clean, fast, and way better than writing long loops. 🔹 Dictionary Comprehensions Perfect for quick mappings without messy code. 🔹 Zip() Combine multiple datasets in seconds (super useful in real projects). 🔹 Set Operations The easiest way to remove duplicates instantly. 🔹 Counter Quick frequency analysis without writing extra logic. (There are 10 in total — these are the ones I use the most 👇) When I started, I used to write long, repetitive code for these tasks. Now, these small tricks help me write cleaner and faster solutions every day. Recently, I’ve been applying many of these while building a real-world order validation pipeline project, and the difference in efficiency is huge. 💡 Small improvements like these compound over time. 📌 Save this post for later — you’ll definitely need it. #Python #DataAnalysis #DataScience #DataEngineering #Analytics #Coding #LearnPython #TechCareers
To view or add a comment, sign in
-
-
I wasted HOURS writing bad Python code when I started in Data Analysis… These simple tricks changed everything 👇 🔹 List Comprehensions Clean, fast, and way better than writing long loops. 🔹 Dictionary Comprehensions Perfect for quick mappings without messy code. 🔹 Zip() Combine multiple datasets in seconds (super useful in real projects). 🔹 Set Operations The easiest way to remove duplicates instantly. 🔹 Counter Quick frequency analysis without writing extra logic. (There are 10 in total — these are the ones I use the most 👇) When I started, I used to write long, repetitive code for these tasks. Now, these small tricks help me write cleaner and faster solutions every day. Recently, I’ve been applying many of these while building a real-world order validation pipeline project, and the difference in efficiency is huge. 💡 Small improvements like these compound over time. 📌 Save this post for later — you’ll definitely need it. #Python #DataAnalysis #DataScience #DataEngineering #Analytics #Coding #LearnPython #TechCareers
To view or add a comment, sign in
-
-
Recursion used to confuse me a lot… until I started thinking of it like a corporate manager delegating work. 😅 🌳 Maximum Depth of Binary Tree (LeetCode 104 — Easy | Blind 75) Whenever I saw a Binary Tree problem, I used to panic. Keeping track of depth while moving through the tree felt overwhelming. The question was always: 👉 How do I keep track of everything at once? I realized something simple but powerful: 👉 I don’t need to track everything myself — I can ask my subtrees Think of each node as a Manager. The manager asks the left subtree: “What’s your max depth?” Then asks the right subtree: “What’s your max depth?” Takes the maximum of both and adds 1 (for itself) That’s exactly what this does: 👉 1 + max(left_depth, right_depth) 🔑 Key Learnings: ✔️ Base Case is your foundation If there’s no node, return 0 → Like an employee saying: “No team under me.” ✔️ Trust Recursion Don’t try to track every path manually → Assume subproblems are solved correctly ✔️ Recognize the Pattern This is a classic Depth-First Search (DFS) approach (bottom-up) ⚙️ Complexity Time Complexity: O(N) — visit each node once Space Complexity: O(H) — recursion stack (tree height) Trees used to be my biggest fear… Now they’re becoming one of my favorite topics Curious to hear from you 👇 What’s a data structure or concept that finally “clicked” for you recently? #LeetCode #BinaryTrees #Blind75 #DataStructures #Recursion #DFS #ProblemSolving #CodingJourney #Python #TechCareers
To view or add a comment, sign in
-
-
M𝗼𝘀𝘁 𝗽𝗲𝗼𝗽𝗹𝗲 𝘄𝗿𝗶𝘁𝗲 𝗣𝘆𝘁𝗵𝗼𝗻. 𝗩𝗲𝗿𝘆 𝗳𝗲𝘄 𝘄𝗿𝗶𝘁𝗲 𝗶𝘁 𝘄𝗲𝗹𝗹. That’s the difference between getting things done… and standing out. You don’t need more tutorials. 𝗬𝗼𝘂 𝗻𝗲𝗲𝗱 𝗯𝗲𝘁𝘁𝗲𝗿 𝗵𝗮𝗯𝗶𝘁𝘀. 𝗛𝗲𝗿𝗲 𝗮𝗿𝗲 𝟭𝟬 𝗣𝘆𝘁𝗵𝗼𝗻 𝘁𝗿𝗶𝗰𝗸𝘀 𝘁𝗵𝗮𝘁 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗺𝗮𝗸𝗲 𝗮 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲: 1. 𝗟𝗶𝘀𝘁 𝗖𝗼𝗺𝗽𝗿𝗲𝗵𝗲𝗻𝘀𝗶𝗼𝗻𝘀 Write less. Do more. 2. 𝗦𝘄𝗮𝗽 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 No temp variable. Cleaner logic. 3. 𝗲𝗻𝘂𝗺𝗲𝗿𝗮𝘁𝗲() Stop managing indexes manually. 4. 𝘇𝗶𝗽() Handle multiple lists effortlessly. 5. 𝘀𝗲𝘁() Remove duplicates in one line. 6. 𝗱𝗶𝗰𝘁.𝗴𝗲𝘁() Avoid crashes. Use defaults. 7. 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗼𝗿𝘀 Handle large data without memory issues. 8. 𝗰𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 Smarter data handling (Counter, defaultdict, deque) 9. 𝗩𝗶𝗿𝘁𝘂𝗮𝗹 𝗘𝗻𝘃𝗶𝗿𝗼𝗻𝗺𝗲𝗻𝘁𝘀 Keep your projects clean. 10. 𝗕𝘂𝗶𝗹𝘁-𝗶𝗻 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 Let Python do the heavy lifting. 𝗕𝘂𝘁 𝗵𝗲𝗿𝗲’𝘀 𝘁𝗵𝗲 𝗿𝗲𝗮𝗹 𝗴𝗮𝗺𝗲: Readable code > clever code Consistency > shortcuts Projects > theory 𝗞𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Python doesn’t reward complexity. 𝗜𝘁 𝗿𝗲𝘄𝗮𝗿𝗱𝘀 𝗰𝗹𝗮𝗿𝗶𝘁𝘆. 𝗖𝗼𝗻𝗻𝗲𝗰𝘁 𝘄𝗶𝘁𝗵 𝗺𝗲 𝗖𝗼𝗺𝗺𝗲𝗻𝘁 𝗮𝗻𝗱 𝗜’𝗹𝗹 𝘀𝗵𝗮𝗿𝗲 𝗺𝗼𝗿𝗲 𝘀𝘂𝗰𝗵 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝘁𝗶𝗽𝘀 #Python #PythonTips #Programming #Coding #SoftwareDevelopment #Developers #TechCareers
To view or add a comment, sign in
-
“Day 5 – I built automatic report generator using Python” Today I worked on: Create current directory: base_dir = os.path.dirname(os.path.abspath(__file__)) Create current path: report_path = os.path.join(base_dir, '..', 'data', 'report.txt') Open file and write report Using loop on all students to find weak students WHAT BUILT TODAY A real report system Exactly what SaaS tools do Facing challenge: Problem: “Weak Students” is repeating inside the subject loop. Loop runs 3 times (math, english, science). Each time it prints: one subject then “Weak Students” Solution: separate the sections. Inside loop = runs multiple times, Outside loop = runs once What I learned : Generate a student report file Save it as .txt Summarize insights (topper, weak students, averages) Understand every line I am documenting my journey to becoming a Data Scientist while building real-world projects. #DataScience #Python #SaaS #Automation #Analytics #BuildInPublic
To view or add a comment, sign in
-
-
Best for: Posting late at night or early morning to show your dedication. Headline: While the world sleeps, the code keeps building. 🌙 It’s been a high-energy day with 83+ profile visits. People are asking: "What are you building in that 2024 repo?" The answer is simple: The future of my workflow. I am documenting my journey of turning raw Python scripts into scalable, automated solutions. From smart profiling to interactive Plotly dashboards, I’m building a library that works for me, so I don't have to work twice. If you haven’t seen it yet, come take a look and see why the tech community is stopping by my profile. ⭐ Support the work on GitHub: 🔗 https://lnkd.in/dGvJaB7a #DataScience #Hustle #Coding #Python #Automation #Shafiq73 #GitHub
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development