Citadel interview question

1. How to reverse a linked list

Interview Answer

Anonymous

May 1, 2020

function reverse(head) { if (!head.next) { return head } let prev = null let current = head let next = null while (current) { next = current.next current.next = prev prev = current current = next } return prev } function reverseRecursive(head, prev=null) { if (!head.next) { return head } let next = head.next let head.next = prev return reverseRecursive(prev, next) }