Fork me on GitHub

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

JavaScript 链表定义

function ListNode(x){
    this.val = x;
    this.next = null;
}
function ReverseList(pHead)
{
  
  	if(pHead === null || pHead.next === null) return pHead
    let pre = null 
    let cur = pHead
    let nex 
    while(cur){
      	nex = cur.next
      	cur.next = pre
      	pre = cur
        cur = nex
    }
    return pre
}

var arr = [1,2,3,4,5]
var head 
var dd = new ListNode(1)
head = dd
for(let i=0 ;i<arr.length-1;i++){
  dd.next = new ListNode(arr[i+1])
  dd = dd.next
}
ReverseList(head)
posted @ 2020-11-09 00:12  365/24/60  阅读(250)  评论(0编辑  收藏  举报