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?
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.
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)
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
@@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.
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
Your attention span and focus is incredible. I got the idea but I could never dissect it the way you could. Good video. Thanks.
true
you make such great videos. really helping me prepare for my interview
thank you!
dude hell yes thank you
Thanks for such great videos
perfect explenation
but what if the size is 17 for example.
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?
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.
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)
Plausibly. Why don't you test it and see?
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
if number of elements is out of range integer type your way will fail
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?
Generally speaking, we're assuming when we say linked list that it won't contain cycles
can you make one of splitting a list using arrays?
I'm not sure I totally understand the question? Why would you want to use arrays?
What language is this in?
All of my videos are in Java
Why can't we compute the midpoint of the list and split it right there?
*Using a fast pointer and a slow pointer!!*
That's what we're doing...
@@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.
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
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()
Why do I have to see algoexpert ad inbetween such quality video.So irritating.