This article is participating in the nuggets team number online activity, click to see the dachang spring recruiting positions

I. Title Description:

Reverse a single linked list.

Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULLCopy the code

Advancements: You can iterate or recursively reverse linked lists. Can you solve the problem in two ways?

Ii. Analysis of Ideas:

Using recursive methods, nothing complicated, notice that each layer of recursion to a new ListNode, and then pass it to the next layer, okay

Iii. AC Code:

/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ const recursion = function(node,previous){ current = new ListNode(node.val,previous) if(node.next===null){ return current }else{ return recursion(node.next,current) } } var reverseList = function(head) { if (! head) return head return recursion(head,undefined) };Copy the code

Iv. Summary:

The iterative method is to be supplemented