g | x | w | all
Bytes Lang Time Link
080Go240612T212142Zbigyihsu
010Pip xp240311T213827ZDLosc
009Pip x240311T212411ZDLosc
065Perl 5 MListUtil=min240311T153851ZXcali
005Vyxal 3240106T202213Zpacman25
009Uiua240106T200949Zchunes
007Japt g230117T080520ZShaggy
042Arturo230117T072150Zchunes
028Prolog SWI220903T054447ZAiden Ch
021Curry PAKCS220405T124205ZWheat Wi
139Desmos220327T110155ZAiden Ch
006Vyxal220321T154659ZSeggan
068C gcc220317T031538ZErikF
025Julia 1.0220315T093217ZMarcMush
045PowerShell Core220313T205621ZJulian
032Ruby220314T091307ZG B
00505AB1E220313T125112ZKevin Cr
027Bash220314T020636ZJonah
038PARI/GP220314T010009Zalephalp
065HBL220313T213301ZDLosc
036R220313T123129Zpajonk
nanJ220313T170105ZJonah
039x86 machine code 32bit220313T163625Zxiver77
005Husk220313T145956ZDominic
033JavaScript ES6220313T150734ZArnauld
039Factor220313T143433Zchunes
005Jelly220313T133532ZJonathan
051Perl 6220313T131551ZRazetime
016APL Dyalog Unicode220313T131405ZAdá
032Python 2220313T104543Zpxeger
019Haskell220313T105459Zpxeger
013Charcoal220313T113209ZNeil
027APL+WIN220313T111124ZGraham
040Retina 0.8.2220313T105223ZNeil

Go, 80 bytes

func f(a,b[]int)(o[]int){if len(a)<1{return}
return append(a[:1],f(b,a[1:])...)}

Attempt This Online!

Straight-forward solution directly implementing the algorithm in the OP.

Pip -xp, 10 bytes

FI\$&FLZDg

Takes two Pip lists as command-line arguments; outputs a Pip list to stdout. Try It Online!

Explanation

Port of Jonah's J answer, among others:

FI\$&FLZDg
         g  ; List of command-line args (i.e. list of lists of numbers)
       ZD   ; Zip (transpose), filling with a default value of "" (which is falsey)
     FL     ; Flatten
  \$&       ; Scan on logical AND, making everything after the first falsey value
            ; falsey
FI          ; Filter, keeping only truthy values

Pip -x, 9 bytes

Wb::aPPOb

Takes two Pip lists as command-line arguments; outputs to stdout, one number per line. Attempt This Online!

Explanation

Wb::aPPOb
           ; a and b are command-line args
W          ; Loop while
 b::a      ; b, after being swapped with a, is truthy (nonempty):
      POb  ;   Pop the first element from b
     P     ;   and print it

Perl 5 -MList::Util=min, 65 bytes

sub{($a,$b)=@_;grep/./,map{$_,shift@$b}@$a[0..min(@$b+0,$#{$a})]}

Try it online!

Vyxal 3, 5 bytes

Zf0sh

Try it Online!

takes input in reverse order

Uiua, 9 bytes SBCS

↙⊗0.♭⍉⬚0⊟

Try it!

Port of Jonah's J answer.

Japt -g, 7 bytes

íV c óÏ

Try it

íV c óÏ     :Implicit input of arrays U & V
íV          :Interleave U with V, padding V with null
   c        :Flatten
     ó      :Partition before elements
      Ï     :That are falsey
            :Implicit input of first element

5 bytes

By taking input as a 2D array, with the order reversed.

ÕcÔóÏ

Try it

ÕcÔóÏ     :Implicit input of 2D array
Õ         :Transpose, padding the first array with null
 c        :Flat map
  Ô       :Reverse
   óÏ     :As above
          :Implicit input of first element

Arturo, 42 bytes

f:$[a,b][([]=a)?->a->@[a\0]++f b drop a 1]

Try it

f:$[a,b][          ; a function f taking two lists a and b
  ([]=a)?          ; is a empty?
  ->a              ; if so, return a
  ->               ; otherwise,
    @[a\0]         ; the first element of a as a list
    ++             ; concatenated with
    f b drop a 1   ; the result of calling f with b and a without its first element
]                  ; end function

Prolog (SWI), 37 28 bytes

-9 bytes thanks to @DLosc

[H|T]+B+[H|Y]:-B+T+Y.
A+_+A.

Try it online!

Port of the Haskell solution.

Curry (PAKCS), 21 bytes

(a:b)#c=a:c#b
[]#_=[]

Try it online!

The Haskell answer almost works in Curry, but because of non-determinism it matches any prefix of the result. We can restrain it by adding two more bytes.

Desmos, 139 bytes

l=[1...a.length+b.length]
g(A)=join(A,0l)[ceil(l/2)]
L=\{\mod(l,2)=1:g(a),g(b)\}
f(a,b)=\{a.\length=0:a,L[1...\min(\{L=0:l,l.\max+1\})-1]\}

Holy crap, it took me forever for me to get an actually working solution for this challenge in Desmos. I don't know if I'm severely overcomplicating this or what, but I got it to work so...

¯\_(ツ)_/¯

I'll see if I can golf this some more later, too tired to think about this right now :P

Try It On Desmos!

Try It On Desmos! - Prettified

Vyxal, 6 bytes

Zf0€÷^

Try it Online!

Looks like 05AB1E beats Vyxal this time. Takes input in reversed order.

Zf0€÷^ # Leaving out this comment makes the explanation kinda feel empty
Z      # Zip the lists
 f     # Flatten
  0€   # Split on 0
    ÷  # Push every item
     ^ # Reverse the stack

Vyxal, 6 bytes

Zf:0ḟẎ

Try it Online!

A slightly different approach. Still 6 bytes.

Zf:0ḟẎ # Leaving out this comment makes the explanation kinda feel empty
Z      # Zip the lists
 f     # Flatten
  :    # Duplicate the top of the stack
   0ḟ  # Index of the first 0
     Ẏ # Take everything up until that index

Vyxal, 5 bytes

Zf0€h

Try it Online!

Takes input in reversed order. Returns 0 for the case of 2 empty lists, so does not meet the spec.

Zf0€h # Leaving out this comment makes the explanation kinda feel empty
Z     # Zip the 2 lists
 f    # Flatten them
  0€  # Split on 0
    h # Get the first item

C (gcc), 68 bytes

Takes 0-terminated arrays as input.

f(a,b)int*a,*b;{for(;*a;!*b?a=b:b++)printf("%d %d "+3*!*b,*a++,*b);}

Try it online!

Julia 1.0, 25 bytes

a^b=@show(a[1])b^a[2:end]

Try it online!

prints the values and ends with an error

Julia 1.0, 30 bytes

>(b)=[]
>(b,a,A...)=[a;A>b...]

Try it online!

expects b>a..., returns an actual list

PowerShell Core, 45 bytes

param($a,$b)for(;$a){$h,$t=$a
$a,$b=$b,$t
$h}

Try it online!

One test case returns 1 instead of [1] as PowerShell tends to flatten arrays

-1 byte thanks to mazzy

Ruby, 32 bytes

f=->a,b{r,*a=a;r ?[r,*f[b,a]]:a}

Try it online!

05AB1E, 6 5 bytes

ζ˜#R`

Input-lists in reversed order.

1 byte is added to deal with the edge case of two empty input-lists, which otherwise would result in an empty string. Otherwise the two trailing bytes could have been н (pop and push first inner list) instead.

Try it online or verify all test cases.

Explanation:

ζ      # Zip/transpose the two (implicit) input-lists, with space " " as filler
 ˜     # Flatten this list of pairs
  #    # Split it on spaces
   R   # Reverse the list of lists
    `  # Pop and push the lists to the stack
       # (after which the top list is output implicitly,
       # or the (implicit) input-list if both lists were empty)

Bash, 27 bytes

read x<$1&&echo $x&&f $2 $1

Try it online!

Port of pxeger's python solution to Bash.

Input lists are taken as files (one entry per line). Output is one entry per line. Output contains extra blanks, which may be illegal and could be fixed at the price of some bytes, but I liked the simplicity of the translation.

PARI/GP, 38 bytes

f(a,b)=if(a,concat(a[1],f(b,a[^1])),a)

Attempt This Online!

a[^1] means skiping the first item in a.

HBL, 6.5 bytes

?.(211.'?,(2.

The branch macro isn't implemented in the online interpreter at the moment, so here's a 7.5-byte equivalent without it:

?.(1(1.)('?,(2.

Try it!

Explanation

The longer version implements the spec directly:

?.(1(1.)('?,(2.
?                If
 .               The first argument is truthy:
  (1               Cons
    (1.)           The head of the first argument to
        ('?        A recursive call with
           ,       The second argument and
            (2.    The tail of the first argument
                 Otherwise, return the first argument (empty list)

The branch macro (2, in golfed HBL) takes several expressions and restructures them into a function call with two arguments. With six expressions, it works like this:

(2 a b c d e f)  ->  (a (b c) (d e f))

In this case, we have:

2 1 1 . '? , (2.)  ->  (1 (1 .) ('? , (2.)))

R, 36 bytes

f=\(a,b)if(sum(a))c(a[1],f(b,a[-1]))

Attempt This Online!


Slightly longer, but without recursion:

R, 43 bytes

\(a,b,x=rbind(c(a,0),c(b,0)))x[!cumsum(!x)]

Attempt This Online!

J, 19 18 17 16 bytes

0(i.~{.]),@|:@,:

Try it online!

-1 thanks to ovs!

Consider 1 2 3 4 f 8 9:

x86 machine code (32-bit), 39 bytes

\x31\xC0\x89\xC5\x55\x87\x2C\x24\x39\xCD\x74\x19\xFF\x34\xAE\x8F\x04\x87
\x45\x40\x87\x2C\x24\x39\xD5\x74\x0A\xFF\x34\xAB\x8F\x04\x87\x45\x40\xEB
\xE0\x5D\xC3

Try it online!

The algorithm is quite straightforward, but 32-bit x86 really needs more registers. Some ugly stack hacks are used to cope with the lack of registers.

The test cases in TIO relies on a GCC extension supporting C arrays of length 0. ISO C and C++ forbid 0-length arrays.

Since OP didn't specify the upper bound of integers, I tried to find some clever way using an array of bytes, and interleaving them with pdep or some SSE shuffle instruction, but that ended up using too many bytes.

# edi: dst, esi: arr0, ecx: len0, ebx: arr1, edx: len1
# eax: return, ebp: clobber

0:  31 c0                   xor    eax,eax
2:  89 c5                   mov    ebp,eax
4:  55                      push   ebp
5:  87 2c 24                xchg   DWORD PTR [esp],ebp
8:  39 cd                   cmp    ebp,ecx
a:  74 19                   je     0x25
c:  ff 34 ae                push   DWORD PTR [esi+ebp*4]
f:  8f 04 87                pop    DWORD PTR [edi+eax*4]
12: 45                      inc    ebp
13: 40                      inc    eax
14: 87 2c 24                xchg   DWORD PTR [esp],ebp
17: 39 d5                   cmp    ebp,edx
19: 74 0a                   je     0x25
1b: ff 34 ab                push   DWORD PTR [ebx+ebp*4]
1e: 8f 04 87                pop    DWORD PTR [edi+eax*4]
21: 45                      inc    ebp
22: 40                      inc    eax
23: eb e0                   jmp    0x5
25: 5d                      pop    ebp
26: c3                      ret 

Husk, 5 bytes

↑_ΣT0

Try it online!

Input is list of the two lists.

Transposes using 0 to fill shorter rows, flattens (Σ) the result, and takes the longest prefix with all elements that are truthy using the function _ (=negate; it would have been more-intuitive to use the Identity function here, but for reasons that I don't understand this doesn't seem to work).

Pretty-much the same approach as Kevin Cruijssen's answer but independently found.

Note that the Husk interpreter will not accept input consisting of only empty lists, presumably since it's unable to infer their type. The TIO link above successfully runs all the test cases (including the first, only-empty-lists one), since these are grouped together and run side-by-side, allowing the interpreter to infer that all lists are of numbers; but it fails when given the first test case on its own. I can't currently see a way to avoid this.

JavaScript (ES6), 33 bytes

f=([v,...a],b)=>v?[v,...f(b,a)]:a

Try it online!

Factor, 43 39 bytes

[ f pad-longest vmerge { f } split1 . ]

Try it online!

Explanation

                ! { 1 2 3 4 5 } { 11 12 13 }
f               ! { 1 2 3 4 5 } { 11 12 13 } f
pad-longest     ! { 1 2 3 4 5 } { 11 12 13 f f }
vmerge          ! { 1 11 2 12 3 13 4 f 5 f }
{ f }           ! { 1 11 2 12 3 13 4 f 5 f } { f }
split1          ! { 1 11 2 12 3 13 4 } { 5 f }
.               ! { 1 11 2 12 3 13 4 }

Jelly, 5 bytes

Uses Kevin Cruijssen's method from their 05AB1E answer.

z⁶FḲḢ

A monadic Link that accepts a pair of lists, [a, b], and yields a zipped list.

Try it online! Or see the test-suite.

How?

z⁶FḲḢ - Link: pair of lists [a,b]
 ⁶    - space character
z     - transpose [a,b] with space-filler
  F   - flatten
   Ḳ  - split at spaces
    Ḣ - head

Perl 6, 51 bytes

sub f(@a,@b){if @a {@a[0],|f(@b,@a[1..*])}else{()}}

Try it online!

APL (Dyalog Unicode), 16 bytes

Anonymous tacit prefix function taking a list of two lists.

((>+2×⌊)/≢¨)⍴⍉∘↑

Try it online!

Python 2, 32 bytes

def f(X,Y):print X.pop(0);f(Y,X)

Attempt This Online!

Ends with an error.

Python, 35 bytes

f=lambda X,Y:X and X[:1]+f(Y,X[1:])

Attempt This Online!

No error.

Whython, 33 bytes

f=lambda X,Y:[X.pop(0)]+f(Y,X)?[]

Attempt This Online!

Haskell, 19 bytes

(a:x)#y=a:y#x
o#_=o

Attempt This Online!

Charcoal, 13 bytes

UMθ⮌ιW§θⅉ⟦I⊟ι

Try it online! Takes input as a pair of lists. Explanation:

UMθ⮌ι

Reverse each of the two lists.

W§θⅉ

Cyclically index the lists according to the number of values already output and repeat until that list is empty.

⟦I⊟ι

Output the "next" value from that list on its own line.

APL+WIN, 27 bytes

Prompts for two lists as a nested vector

(^\0≠v)/v←,⍉⊃(⌈/⍴¨v)↑¨v←,¨⎕

Try it online! Thanks to Dyalog APL Classic

Retina 0.8.2, 40 bytes

^
;
+`;(\d+),?(.*);(.*)
$1,;$3;$2
,?;.*

Try it online! Link includes test cases. Explanation:

^
;

Start with an empty output list.

+`

Until the first input list is empty, ...

;(\d+),?(.*);(.*)

... match the first element of the first input list, the rest of the first input list and the second input list, and...

$1,;$3;$2

... move the first element of the input list to the output list and swap the rest of the first input list with the second list.

,?;.*

Delete the input lists.