## 分析

val属性存储当前的值，next属性存储下一个节点的引用。

## 代码

/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function printListFromTailToHead(head)
{
const result = [];
let temp = head;
while(temp){
result.push(temp.val);
temp = temp.next;
}
return result.reverse();
}

## 拓展

function LinkList(){
function node(element){
this.value = element;
this.next = null;
}
let length = 0;
let head = null;
}
LinkList.prototype = {
// 追加
append:function(element){
var node = new node(element);
var temp = this.head;
if(this.head){
//遍历找到链表的终点
while(temp.next){
temp = temp.next;
}
temp.next = node;
}else{
this.head = node;
}
this.length++;
},
// 插入
insert:function(element,index){
if(index <= this.length && index>0){
var node = new node(element);
var currentIndex = 0;
var currentNode = this.head;
var preNode = null;
if (currentIndex === 0) {
node.next = currentNode;
this.head = node;
return;
}
while(currentIndex<index){
preNode = currentNode;
currentNode = currentNode.next;
currentIndex++;
}
preNode.next = node;
node.next = currentNode;
this.length++;
}
}
}

## 链表翻转

/**
* Definition for singly-linked list.
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function (head) {
let currentNode = null;
let headNode = head;
while (head && head.next) {
// 将当前节点从链表中取出
currentNode = head.next;
head.next = currentNode.next;
// 将取出的节点移动到头部
currentNode.next = headNode;
headNode = currentNode;
}
return headNode;
};