<commit_before>// Source : https://leetcode.com/problems/reverse-linked-list/
// Author : <NAME>
// Date   : 2015-06-12

/**********************************************************************************
 *
 * Reverse a singly linked list.
 *
 * click to show more hints.
 *
 * Hint:
 * A linked list can be reversed either iteratively or recursively. Could you implement both?
 *
 *
 **********************************************************************************/

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * Recursion
 *
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList_recursive = function(head) {
    if (!head) {
        return head;
    }
    var next = head.next;
    head.next = null;

    return reverse(next, head);

    function reverse(curr, prev) {
        if (!curr) {
            return prev;
        }
        next = curr.next;
        curr.next = prev;

        return reverse(next, curr);
    }
};

/**
 * Iteration
 *
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList_iterative = function(head) {
    if (!head) {
        return head;
    }
    var node, next;
    next = head.next;
    head.next = null;

    while (next) {
        node = next.next;
        next.next = head;
        head = next;
        next = node;
    }
    return head;
};

/**
 * Main function
 *
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    return new Date().getSeconds() % 2 ? reverseList_recursive(head) : reverseList_iterative(head);
};<commit_msg>Update ReverseLinkedList.js<commit_after>// Source : https://leetcode.com/problems/reverse-linked-list/
// Author : <NAME>
// Date   : 2015-06-12

/**********************************************************************************
 *
 * Reverse a singly linked list.
 *
 * click to show more hints.
 *
 * Hint:
 * A linked list can be reversed either iteratively or recursively. Could you implement both?
 *
 *
 **********************************************************************************/

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * Recursion
 *
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    if (!head || !head.next) return head

    let node = reverseList(head.next)
    
    head.next.next = head
    head.next = null
    
    return node
};

/**
 * Iteration
 *
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    let [prev, next] = [null, null]
    while (head) {
        next = head.next
        head.next = prev
        prev = head
        head = next
    }
    return prev
};
{'fraction_non_alphanumeric': 0.24120603015075376, 'fraction_numerical': 0.006101938262742283, 'mean_word_length': 2.6622864651773983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6118691', 'n_tokens_mistral': 864, 'n_tokens_neox': 826, 'n_words': 217}