Merge in Between Linked Lists - Leetcode 1669 - Python
Key Takeaways
The video demonstrates a solution to the Leetcode problem 1669, Merge in Between Linked Lists, using Python. The problem involves merging two linked lists by removing a section of nodes from the first list and inserting the second list in its place.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem merge in between linked lists so let's just focus on the example so straight to it we're given two link lists as you can see here and we're also given two integers here A and B so in this case if a is equal uh to 3 and b is equal to 4 what that means is we want to go to the third node and this is actually they don't mention it anywhere in the problem unless I missed it this is assuming this is zerob based so 0 1 2 3 4 and five so you can see from 3 to 4 we want to remove those nodes from 3 to four okay so that's not hard and in between like where that was we want to now insert the new linked list so as you can see this last node the node before the node that we removed is going to then point at the beginning of list two and as you can see the last node in list two is going to point at the node after the section that we removed and not only that we are actually guaranteed they kind of mention it down in the footnotes but we're pretty much guaranteed that a to be like this section that we remove is not going to include the head of the link list so that makes it really really convenient for us to be honest and so the way I'm going to go ahead and implement the solution to this problem is pretty much what I just talked about right now we're going to have a pointer that is going to pretty much iterate up until this node and then we're just going to have it point at list two then we're going to continue to iterate until we have a pointer to this node and lastly we're going to Traverse the entire second list going through that entire list so that then we can make the last uh node point at this node so pretty much we're going to go through both of the lists you can assume that the time complexity is n plus M where these are the sizes of the lists that they mention and no additional space complexity so it's going to be constant space The Reason by the way that this was such an easy explanation for me to give is that they actually gave us a very very good picture in this problem so much so that I would consider this actually more of an easy problem and the big takeaway I think from this problem is that pictures are so damn helpful for understanding these problems right that's kind of why I do this whiteboarding stuff they gave us the picture this time but usually you have to create it yourself so for harder problems this kind of problem solving skill of being able to draw out a picture is extremely helpful and I didn't really have a lot of notes uh this time for the problem to show you guys so let's just jump into the code so first I'm just going to have a pointer it's going to be at the beginning of list one we want this to point at the node before the node that we want to remove so I'm going to have a pointer and I'm actually going to set it to one because I want to say this while I is less than a we want to do this we want to shift the pointer and we want to increment I and the reason I set it to one is just because that's how the math works out like if I show you a linked list that let's say looks like this if a is uh three for example if a is three that means we want to remove starting from this node right because we're zero indexed and now that I think of it actually maybe a better way is to actually change this to a zero and then change this to a minus one so then pretty much what we're saying is I is going to represent the index which I think that's probably the more intuitive thing to do and then we're saying okay keep incrementing until I has reached the node that is right before a so if a is here we are going to stop once we reach this node which is exactly what we want to do so I think this is probably the better way to do it so now by the time this Loop is is done current should be pointing here and so even though now we want to set the next node of this to list two we can't do that just yet because we do want to iterate over the rest of this so we will write that code we'll say here that head is equal to a snapshot of what current is right now so head will basically point to this node and then eventually we're going to say head. next is equal to list two but we're going to save that for later so in between we're going to add some more code here because we want to Traverse the rest of this or at least up until uh B so we now want to get a pointer to this node or rather the node after that cuz we want to remove this section here so let me just make that very clear this is the section we're trying to remove and so we want a pointer here because we want the last node in list two to point here so let's say uh while I is less than or equal to B actually because if we say less than b we will stop here but we want to stop after that so we will continue one more time after that and then stop here so here let's just say current is equal to current. next and I + 1 and so now not only can we point this at the beginning of list two but now we can also iterate over list two just like this this is the easy part so while list two is non-null we can increment this or rather shift it to the next node like this but this is actually not good what this is going to do is it's going to keep shifting this pointer until we reach null list two will point at null that's not what we want we want the last node from list two so let's actually say while list 2. next is non-null and so this will stop at the last node and then with the last node we can say list 2. next is equal to tail and then we can return the head of list one which we we did not change and I don't know why actually I pointed this at tail we didn't Define tail so I guess I will change this to curve because remember after this Loop current is pointing at the node right after the one that we deleted so now let's go ahead and run the code and as you can see it works it's pretty efficient if you're preparing for coding interviews check out NE code. thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: https://twitter.com/neetcode1
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
Problem Link: https://leetcode.com/problems/merge-in-between-linked-lists/
0:00 - Read the problem
0:30 - Drawing Explanation
2:40 - Coding Explanation
leetcode 1669
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (3)
Read the problem
0:30
Drawing Explanation
2:40
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI