Interview Algorithm Challenge: Reverse Linked List - Swift

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ •

  • @rogerwprice
    @rogerwprice 7 років тому +2

    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...

  • @Bhaukaal-YT-001
    @Bhaukaal-YT-001 3 роки тому +2

    Good Work, It's some time difficult to find Swift based algo and DS implementions. Thank you.

  • @scottlydon4631
    @scottlydon4631 5 років тому +1

    I put this implementation in LeetCode and it ran 24ms faster than 100% of Swift submissions...WOW.

  • @lelouchlamperouge6273
    @lelouchlamperouge6273 7 років тому

    Thank you for the video Brian! Very helpful.

  • @saranggurav5677
    @saranggurav5677 6 років тому

    Very Helpful, Thanks Brian!

  • @saudtomtit
    @saudtomtit 7 років тому +1

    Thanks for sharing this. What is your editor API?

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому +2

    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
    }

    • @rajeshm8493
      @rajeshm8493 4 роки тому

      Awesome! how about doubly linked list?

    • @PythonPlusPlus
      @PythonPlusPlus 4 роки тому

      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.

    • @dre5671
      @dre5671 2 роки тому

      This is beautiful. Wish I could understand it

    • @PythonPlusPlus
      @PythonPlusPlus 2 роки тому +1

      @@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

    • @dre5671
      @dre5671 2 роки тому

      @@PythonPlusPlus Oh wow thanks for taking the time to spell it out! I’ll mediate on it.

  • @johnnyAustria80
    @johnnyAustria80 7 років тому +3

    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;
    }

  • @MuratBirgul
    @MuratBirgul 7 років тому +1

    thanks Brian.. awesome...

  • @cindyyamaguchi2336
    @cindyyamaguchi2336 7 років тому +4

    Is prev null at the start of the first interaction?

  • @darkoms
    @darkoms 7 років тому

    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

  • @ThePacket10
    @ThePacket10 7 років тому

    congrats to you, great content!!!

  • @DigitalOnionNews
    @DigitalOnionNews 7 років тому +1

    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?

    • @DigitalOnionNews
      @DigitalOnionNews 7 років тому +1

      just not understanding the head variable at all in this program

    • @youtubeaddict6128
      @youtubeaddict6128 Рік тому +1

      If you want to reverse from middle then send head as any middle node

  • @Sacrious
    @Sacrious 7 років тому

    Thanks for sharing all your knowledge. Which ORM for sqlite data handling do you use?

  • @jairoeli2305
    @jairoeli2305 7 років тому +1

    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!

    • @Erlisch1337
      @Erlisch1337 7 років тому

      Should be ok since what he wrote here is basiclly the same thing, just without a function/method call.

    • @w0mblemania
      @w0mblemania 7 років тому

      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.

  • @1314520Joanna
    @1314520Joanna 7 років тому

    what is your home office lighting for working with papers and computers? Any recommendation for lighting to improve productivity and reduce eye constrain?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      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.

    • @neeleshshah
      @neeleshshah 7 років тому

      try justgetflux.com/

  • @wensmusic8636
    @wensmusic8636 2 роки тому

    Does this solution still work in 2022? i tried using your code's solution but im getting a ton of errors

  • @emathew97
    @emathew97 7 років тому

    How is you mike and camera setup, great audio by the way!

  • @krushnaakhade4678
    @krushnaakhade4678 Рік тому

    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

  • @victorwu1654
    @victorwu1654 7 років тому +1

    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.

  • @ronyfhebrian2629
    @ronyfhebrian2629 7 років тому

    Why you're not using struct Node instead of class Node? struct keeps the value instead of class keeps the reference right? Thanks

  • @MuhammadAli-zv5vz
    @MuhammadAli-zv5vz 7 років тому

    please upload a video how to connect app with mysql using swift3

  • @ryantin9497
    @ryantin9497 7 років тому

    What language and what compiler is this?

    • @w0mblemania
      @w0mblemania 7 років тому

      Swift, and Xcode's swift compiler.

  • @shaikbaji1506
    @shaikbaji1506 7 років тому

    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

  • @nikinlug
    @nikinlug 7 років тому +3

    Why wouldn't you just use recursion?

    • @steevineer
      @steevineer 7 років тому +1

      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)
      }
      }

    • @nikinlug
      @nikinlug 7 років тому

      I know how it's done, I just wondered why he went less obvious way

    • @steevineer
      @steevineer 7 років тому +1

      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.

    • @nikinlug
      @nikinlug 7 років тому +1

      Stephen Nguyen it does tail call optimization.

    • @steevineer
      @steevineer 7 років тому

      Great! Cheer!

  • @kennygrandberry8590
    @kennygrandberry8590 7 років тому

    nice!! I found a different way to work with linklist but yours is much shorter

  • @dre5671
    @dre5671 2 роки тому

    I am just racking my brain over this solution. I just cant seem to understand it!!! Argh!

  • @MuhammadAli-zv5vz
    @MuhammadAli-zv5vz 7 років тому

    it is like bubble sorting ............

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому

    Why would you ever want to use a linked list?

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      This question is best suited for google young lad.

  • @SepaloidTV
    @SepaloidTV 7 років тому +2

    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.

    • @w0mblemania
      @w0mblemania 7 років тому +2

      Had to laugh at this post. I sure hope it was deliberate comedy.

    • @levyshi
      @levyshi 7 років тому

      what the hell