Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-063

Split Linked List in Half

Problem

Split a singly linked list into two halves.

When the list has an odd number of nodes, the first half receives the extra node, so it gets the first ceil(n/2) values and the second half gets the rest.

Return [first_half, second_half], where either may be empty.

Hint

The split point is (n + 1) // 2, which gives the first half one extra node when n is odd.

Test cases

case 1
in: [[1,2,3,4,5]]
expected: [[1,2,3],[4,5]]
case 2
in: [[1,2,3,4]]
expected: [[1,2],[3,4]]
case 3
in: [[]]
expected: [[],[]]
case 4
in: [[1]]
expected: [[1],[]]
Starter codePython
def split_list(values):
    # Your code here
    pass

4 test cases · run your code against them in the editor