Nice - had not thought of this! Indeed it is obvious when you explained that the "trick" is to just traverse the list normally and reverse the pointers before returning the last item as the new head! These are GREAT - thanks...
Here's a recursive function I came up with: func reverseListRecursive(head: Node) -> Node { guard let next = head.next else { return head } let newHead = reverseListRecursive(head: next) next.next = head head.next = nil return newHead }
@@dre5671 Here’s a visualisation: Assume a linked list: head, next, a, b, c, … First, check if the list is only 1 item, return the head if that is the case, otherwise get a reference to the “next”: next, a, b, c, … Then reverse next and it’s children: …, c, b, a, next Then make head a child of next: …, c, b, a, next, head, next, head, next, … Then ensure that the heads child is nil, since it still references next, and creates an infinite loop: …, c, b, a, next, head
Hi Brian, really enjoying these algorithm videos...(actually all your videos are great). Just curious at how the efficiency of a while loop compares with a recursive method like below. Not going to claim this as my own, and understanding how it works has made me want to pull my eyes out :) func reverseList(head: Node?) -> Node?{ if head?.next == nil { return head } let node = reverseList(head: head?.next) head?.next?.next = head head?.next = nil return node; }
Why use "?? -1" to unwrap instead of just force unwrapping with "!"?. You know for a fact (because that is how you set up the while loop condition) that the Node exists within the loop else it would have already exited. Also, when you flip the links wouldn't the steps look like this: Initial: 1->2->3->nil First loop: nil3->nil Second loop: nil
sorry this may be a dumb question but why do you have head: Node? in the function. You're passing the nodes to be printed but what does the "head:" part do?
I have a way to reverse a generic linked list by using the global Swift function swap() . Is that a valid solution for the technical interview? I'd love to hear your thoughts. Thanks!
Technically it's valid. But then so is foregoing a linked list entirely and simply using an NSArray/Array. And that's not the point of the interview: the interviewer wants to know if you _can_ implement the reverse list design yourself. Whether this is a useful thing to know in the real world is another thing entirely. Personally, I think such questions are probably borderline-pointless in modern app development. But it depends on what kind of work you do.
what is your home office lighting for working with papers and computers? Any recommendation for lighting to improve productivity and reduce eye constrain?
The only thing I can recommend is to get either a dual monitor setup or a 40" 4K monitor which is what I have. This will increase productivity by a lot.
Hey, I like the video but please put an annotation to fix your linked list node around 8:00. nil 2 -> 3 -> nil, this is technically impossible. The linked list will actually be showing up like this: nil 3 -> nil. with next and currentNode being 2 and prev being 1.
i was very happy after seeing your videos but i need now a small help can u please solve my question ? How to make a login form and register form in swift 3, Xcode 8, iOS 10, You use any database like Sqlite or firebase i need to see the data storing inside that for registering the page and login page and it should login the page by the data which we entered or else it should show the error like password does not matches so can u please make it simple " how to create a login form and register form " ? Please reply me Thank You in Advance
i wanna learn to code so fucking badly, but dont wanna spend the time to learn... shit looks boring to learn, i just want to wake up with code implemented into my brain.
Nice - had not thought of this! Indeed it is obvious when you explained that the "trick" is to just traverse the list normally and reverse the pointers before returning the last item as the new head! These are GREAT - thanks...
Good Work, It's some time difficult to find Swift based algo and DS implementions. Thank you.
I put this implementation in LeetCode and it ran 24ms faster than 100% of Swift submissions...WOW.
Thank you for the video Brian! Very helpful.
Very Helpful, Thanks Brian!
Thanks for sharing this. What is your editor API?
It's XCode Playground
Here's a recursive function I came up with:
func reverseListRecursive(head: Node) -> Node {
guard let next = head.next else {
return head
}
let newHead = reverseListRecursive(head: next)
next.next = head
head.next = nil
return newHead
}
Awesome! how about doubly linked list?
Rajesh M A doubly linked list will be very easy. You just have to swap the references to the next and previous node for every node.
This is beautiful. Wish I could understand it
@@dre5671 Here’s a visualisation:
Assume a linked list:
head, next, a, b, c, …
First, check if the list is only 1 item, return the head if that is the case, otherwise get a reference to the “next”:
next, a, b, c, …
Then reverse next and it’s children:
…, c, b, a, next
Then make head a child of next:
…, c, b, a, next, head, next, head, next, …
Then ensure that the heads child is nil, since it still references next, and creates an infinite loop:
…, c, b, a, next, head
@@PythonPlusPlus Oh wow thanks for taking the time to spell it out! I’ll mediate on it.
Hi Brian, really enjoying these algorithm videos...(actually all your videos are great).
Just curious at how the efficiency of a while loop compares with a recursive method like below. Not going to claim this as my own, and understanding how it works has made me want to pull my eyes out :)
func reverseList(head: Node?) -> Node?{
if head?.next == nil {
return head
}
let node = reverseList(head: head?.next)
head?.next?.next = head
head?.next = nil
return node;
}
thanks Brian.. awesome...
Is prev null at the start of the first interaction?
Iteration
+Cindy Yamaguchi with an optional declaration the default value is null yes
Why use "?? -1" to unwrap instead of just force unwrapping with "!"?. You know for a fact (because that is how you set up the while loop condition) that the Node exists within the loop else it would have already exited.
Also, when you flip the links wouldn't the steps look like this:
Initial: 1->2->3->nil
First loop: nil3->nil
Second loop: nil
congrats to you, great content!!!
sorry this may be a dumb question but why do you have head: Node? in the function. You're passing the nodes to be printed but what does the "head:" part do?
just not understanding the head variable at all in this program
If you want to reverse from middle then send head as any middle node
Thanks for sharing all your knowledge. Which ORM for sqlite data handling do you use?
I have a way to reverse a generic linked list by using the global Swift function swap() . Is that a valid solution for the technical interview? I'd love to hear your thoughts. Thanks!
Should be ok since what he wrote here is basiclly the same thing, just without a function/method call.
Technically it's valid. But then so is foregoing a linked list entirely and simply using an NSArray/Array. And that's not the point of the interview: the interviewer wants to know if you _can_ implement the reverse list design yourself. Whether this is a useful thing to know in the real world is another thing entirely. Personally, I think such questions are probably borderline-pointless in modern app development. But it depends on what kind of work you do.
what is your home office lighting for working with papers and computers? Any recommendation for lighting to improve productivity and reduce eye constrain?
The only thing I can recommend is to get either a dual monitor setup or a 40" 4K monitor which is what I have. This will increase productivity by a lot.
try justgetflux.com/
Does this solution still work in 2022? i tried using your code's solution but im getting a ton of errors
How is you mike and camera setup, great audio by the way!
I enjoy your videos a lot, the only thing could be even helpful if at the time of solving if you could show how it's going in graphical representation
Hey, I like the video but please put an annotation to fix your linked list node around 8:00.
nil 2 -> 3 -> nil, this is technically impossible. The linked list will actually be showing up like this:
nil 3 -> nil.
with next and currentNode being 2 and prev being 1.
Why you're not using struct Node instead of class Node? struct keeps the value instead of class keeps the reference right? Thanks
please upload a video how to connect app with mysql using swift3
What language and what compiler is this?
Swift, and Xcode's swift compiler.
i was very happy after seeing your videos but i need now a small help can u please solve my question ?
How to make a login form and register form in swift 3, Xcode 8, iOS 10, You use any database like Sqlite or firebase i need to see the data storing inside that for registering the page and login page and it should login the page by the data which we entered or else it should show the error like password does not matches so can u please make it simple " how to create a login form and register form " ? Please reply me Thank You in Advance
No.
He has many videos on this, i'd link you but there's a lot.
Why wouldn't you just use recursion?
Mykyta Oliinyk Here it is:
func reverseList(head: Node, prev: Node?) -> Node {
if head.next == nil {
head.next = prev
return head
} else {
let ptr = head.next!
head.next = prev
return reverseList(head: ptr, prev: head)
}
}
I know how it's done, I just wondered why he went less obvious way
You'll get overflow with recursion if the list is large enough. I don't think Swift is tailrec optimized yet, so the loop approach is safer.
Stephen Nguyen it does tail call optimization.
Great! Cheer!
nice!! I found a different way to work with linklist but yours is much shorter
I am just racking my brain over this solution. I just cant seem to understand it!!! Argh!
it is like bubble sorting ............
Why would you ever want to use a linked list?
This question is best suited for google young lad.
i wanna learn to code so fucking badly, but dont wanna spend the time to learn... shit looks boring to learn, i just want to wake up with code implemented into my brain.
Had to laugh at this post. I sure hope it was deliberate comedy.
what the hell