LeetCode Problem 19: "Given the head of a linked list, remove the nth node from the end of the list and return its head." The below implementation is simple and clear- Approach Double traverse the list, once to calculate the length of the list and second time to find the position of the node to be deleted. Maintain two pointers at each iteration, one to keep track present node and second to keep track of previous node. position is calculated by using the formula: pos = (len of list-n)+1 Complexity Time complexity: O(m) where m is length of list Space complexity: O(1) i.e. constant #Python #LeetCode #CompetitiveProgramming #TwoPointers #ProblemSolving #LinkedList #Algorithms #DataStructures
Remove Nth Node from End of Linked List
More Relevant Posts
-
LeetCode Problem 1545: "Given two positive integers n and k, the binary string Sn is formed as follows: S1 = "0" Si = Si - 1 + "1" + reverse(invert(Si - 1)) for i > 1 Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0)." Approach: Initialize an array with a single element '0' which serves as the string1 then iteratively calculate the consecutive strings and store them in the array. Finally, form the final string using the join() function and return the kth character of the string. #Python #LeetCode #String #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithm
To view or add a comment, sign in
-
-
LeetCode Problem 83: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. The below implementation in Python successfully resolves this in time complexity of O(n) where n is length of the list with constant space complexity. The approach is simple, handle the base case first like list having 0 or 1 number of nodes and then write the logic of handling lists of length greater than one. Maintain two pointers, one to keep track of present node and other to keep track of previous node. Check if the value of both nodes match or not, if match update the pointing of previous node, point its next to the present.next. #LeetCode #LinkedList #Python #CompetitiveProgramming #Algorithms #DataStructures #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 2 of #DSAPrep > Problem: Two Sum > Platform: LeetCode > Concept: HashMap/ Dictionary Used a dictionary to store elements and their indices. For each number, checked whether (target - current number) exists in the map to find the required pair. Optimized the brute-force O(n²) solution to O(n). > Time Complexity: O(n) > Space Complexity: O(n) Accepted ✅ Improving logical thinking and efficiency with every problem. #DataStructures #Algorithms #Python #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Problem 1582: "Special Positions in a Binary Matrix": Given an m x n binary matrix mat, return the number of special positions in mat. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). Approach: Initialize two arrays, one stores the number of 1s in each row of the matrix and other stores the number of 1s in each column, by traversing the matrix for filling each of the arrays. Now re-traverse the matrix and if mat[i][j]==1, check for the number of 1s in corresponding row and column, if number of 1s in that row and column is equal to one, we get our required position, calculate the total number of such positions. #Python #LeetCode #Matrices #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithms #Arrays #Coding
To view or add a comment, sign in
-
-
Day 67 of 365 Days of code the infamous brainrot number Qn 1) Reorder list You are given the head of a singly linked-list. The positions of a linked list of length = 7 for example, can intially be represented as: [0, 1, 2, 3, 4, 5, 6] Reorder the nodes of the linked list to be in the following order: [0, 6, 1, 5, 2, 4, 3] Notice that in the general case for a list of length = n the nodes are reordered to be in the following order: [0, n-1, 1, n-2, 2, n-3, ...] You may not modify the values in the list's nodes, but instead you must reorder the nodes themselves. approach: use 5 pointers(scary :")) #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
LeetCode Problem 160: "Intersection of two Linked Lists": Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. Approach: Use two pointers technique, maintain one pointer at list A and another at list B, at each iteration check whether pointers point to the same node, if yes then return that node. If pointer A reaches end update it to point to the head of list B and if pointer B reaches end update it to point to the head of list A. In this way each of the pointers traverses both of the lists. Time complexity: O(m+n) Space Complexity: O(1) #LeetCode #LinkedLists #Python #CompetitiveProgramming #ProblemSolving #Algorithms #DataStructures
To view or add a comment, sign in
-
-
𝗜 𝗳𝗼𝘂𝗻𝗱 𝗮 𝟰𝟰× 𝘀𝗽𝗲𝗲𝗱 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗶𝗻 𝗮 𝘀𝗶𝗺𝗽𝗹𝗲 𝘀𝘂𝗺() 𝗼𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻 🚀 I benchmarked three ways of summing 100,000 numbers: • Manual for loop → ~11.4 ms • Built-in 𝘀𝘂𝗺() → ~8.27 ms • 𝗻𝗽.𝘀𝘂𝗺() → ~0.259 ms 𝗡𝘂𝗺𝗣𝘆 𝘄𝗮𝘀 ~𝟰𝟰× 𝗳𝗮𝘀𝘁𝗲𝗿 𝘁𝗵𝗮𝗻 𝗮 𝗣𝘆𝘁𝗵𝗼𝗻 𝗹𝗼𝗼𝗽 ⚡ The real insight isn’t that “NumPy is faster.” It’s about execution layers. A Python loop runs inside the interpreter with dynamic checks every iteration. 𝘀𝘂𝗺() shifts the work into C. 𝗻𝗽.𝘀𝘂𝗺() operates on contiguous memory using optimized low-level code, avoiding Python-level iteration entirely. Same computation. Different execution layer. Massive performance gap. #Python #NumPy #DataScience #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 42 / 200 – Minimum Window Substring (Hard) Solved LeetCode 76 – Minimum Window Substring today! 💪 This problem was a great test of Sliding Window + HashMap concepts. The challenge was to find the smallest substring that contains all characters of another string (including duplicates). 🔎 Key Learnings: Mastered the two-pointer (left & right) sliding window technique Used frequency counters to track required characters Optimized window shrinking to ensure minimum length Improved understanding of handling duplicate character constraints ✅ 268 / 268 test cases passed ⚡ Runtime: 75 ms 🐍 Language: Python Problems like this strengthen real-world problem-solving skills and improve optimization thinking. Consistency > Motivation. 200 Days. No excuses. 💯 #Day42 #200DaysChallenge #LeetCode #Python #DataStructures #Algorithms #SlidingWindow #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Problem 1404: "Number of steps to reduce a number in binary representation to one": Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules: If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it. Approach: First convert the given binary string to decimal using int() function with base '2'. Then use a while loop, at each iteration check if the number is even divide it by 2, if it is odd add one to it, the loop continues until the number equals to one, a variable "count" initiated to 0 is incremented at each loop, return the "count" variable, simple and straightforward. #LeetCode #Python #Strings #CompetitiveProgramming #ProblemSolving #Algorithms #DataStructures #OptimalSolution #Coding
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode – Day 52 📌 Problem: Sort Colors (LeetCode 75) 💡 Problem Idea: You are given an array containing only 0s, 1s, and 2s, representing three colors. The task is to sort the array in-place so that all 0s come first, then 1s, and then 2s. ⚡ Key Insight: Instead of using a sorting algorithm, we can solve this in one pass using three pointers. We maintain three regions: Left pointer → position for next 0 Right pointer → position for next 2 Current pointer (i) → traverses the array 🎯 Approach (Dutch National Flag Algorithm): If element is 0 → swap with left, move both left and i If element is 1 → just move i If element is 2 → swap with right, decrease right 📈 Complexity: Time Complexity: O(n) Space Complexity: O(1) (in-place sorting) ✅ Efficient because the array is processed only once. 💭 Learning: This problem is a great example of how pointer techniques can optimize sorting problems without using built-in sort functions. #DSA #LeetCode #Python #CodingJourney #ProblemSolving #Algorithms #100DaysOfCode #LinkedInLearning
To view or add a comment, sign in
-
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