Interview Question: Split a Linked List

Sdílet
Vložit
  • čas přidán 13. 09. 2024

Komentáře • 29

  • @MrZZooh
    @MrZZooh Před 4 lety +3

    Your attention span and focus is incredible. I got the idea but I could never dissect it the way you could. Good video. Thanks.

  • @XOGraci09
    @XOGraci09 Před 6 lety +6

    you make such great videos. really helping me prepare for my interview

  • @KoltronZer0
    @KoltronZer0 Před 6 lety +3

    dude hell yes thank you

  • @anujeetswain7368
    @anujeetswain7368 Před 4 lety

    Thanks for such great videos

  • @MrBensella
    @MrBensella Před 3 lety

    perfect explenation

  • @nourchehaytli617
    @nourchehaytli617 Před 2 lety +1

    but what if the size is 17 for example.

  • @rahulnirmesh3388
    @rahulnirmesh3388 Před 4 lety

    You are moving your list Node, so basically you do not have any reference(s) of the previous Node(s) and that's why you cannot say that the divided lists are 1 -> 2 -> 3 -> and 4 -> 5 because according to your equation, list is pointing to 3 at the end, am I correct?

  • @SH-bv9no
    @SH-bv9no Před 3 lety

    Can we also do the while loop this way:
    while(runner != null && runner.next != null)
    {
    runner = runner.next.next;
    list = list.next;
    }
    So it will break out of the loop in the scenario where runner crosses the end of the list.

  • @shayanasgari5594
    @shayanasgari5594 Před 6 lety

    Would this work if your while loop had while(runner!=null && runner.next!=null)
    {
    Runner = runner.next.next
    L = l.next
    }
    With this case we would not set runner to its next prior to entering the loop. We would just set it to the node in the parameter.
    Would this still work?(the same as palindrome algorithm)

    • @ByteByByte
      @ByteByByte  Před 6 lety +1

      Plausibly. Why don't you test it and see?

  • @Grassmpl
    @Grassmpl Před 7 lety

    This problem is much easier if we store the length of the list or compute this length first. Then make a second pass with the appropriate number of nodes

    • @holyshit922
      @holyshit922 Před 7 lety +3

      if number of elements is out of range integer type your way will fail

  • @ronmalka92
    @ronmalka92 Před 5 lety

    Great videos and channel! I have some question, what if there is a cycle in the list? By your sulotion it's never get to null?

    • @ByteByByte
      @ByteByByte  Před 5 lety

      Generally speaking, we're assuming when we say linked list that it won't contain cycles

  • @phambao14
    @phambao14 Před 8 lety

    can you make one of splitting a list using arrays?

    • @ByteByByte
      @ByteByByte  Před 8 lety

      I'm not sure I totally understand the question? Why would you want to use arrays?

  • @RickySolorio
    @RickySolorio Před 8 lety

    What language is this in?

  • @abishek07
    @abishek07 Před 5 lety +1

    Why can't we compute the midpoint of the list and split it right there?
    *Using a fast pointer and a slow pointer!!*

    • @ByteByByte
      @ByteByByte  Před 5 lety +2

      That's what we're doing...

    • @abishek07
      @abishek07 Před 5 lety

      @@ByteByByte
      Sorry sir, I was too quick to suggest my solution without looking into yours.
      Thank you for having created this channel. I am being highly benefited by it.

  • @pursuitofcat
    @pursuitofcat Před 3 lety

    class Node:
    def __init__(self, val):
    self.val = val
    self.next = None
    def __repr__(self):
    return f"Node"
    def splitLinkedList(node: Node):
    slow = fast = node
    while slow is not None and fast is not None:
    if fast.next is None:
    break
    else:
    fast = fast.next.next
    slow = slow.next
    return slow
    one = Node(1)
    two = Node(2)
    three = Node(3)
    four = Node(4)
    five = Node(5)
    one.next = two
    two.next = three
    three.next = four
    # even case
    assert splitLinkedList(one) == three
    # odd case
    four.next = five
    assert splitLinkedList(one) == three

  • @oceanview-u8q
    @oceanview-u8q Před 6 lety

    class Node:
    def __init__(self, val):
    self.value = val
    self.next = None
    def add(self, val):
    if self.next is None:
    self.next = Node(val)
    else:
    self.next.add(val)
    def printTree(self):
    print(self.value)
    if self.next:
    self.next.printTree()
    def divide(self):
    if self.value is None: return None
    runner = self
    fastrunner = self
    while fastrunner:
    print("fasterrunner: ", fastrunner.value)
    print("runner: ", runner.value)
    fastrunner = fastrunner.next
    if fastrunner is None: break
    fastrunner = fastrunner.next
    runner = runner.next
    runner.next = None
    return runner
    c = Node(1)
    c.add(2)
    c.add(3)
    c.add(4)
    c.add(5)
    c.add(6)
    c.add(7)
    c.add(8)
    c.add(9)
    c.add(10)
    c.add(11)
    #c.printTree()
    c.divide()

  • @SyedAli-tc3yu
    @SyedAli-tc3yu Před 3 lety

    Why do I have to see algoexpert ad inbetween such quality video.So irritating.