leetcode.24 javascript解法

1. 题目:

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点

2. 解法:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
  let pre = null;
  let cur = head;

  while(cur) {
    let tmp = cur.next;
    cur.next = pre;
    pre = cur;
    cur = tmp;
  }

  return pre;
};

 

posted @ 2020-04-16 15:25  必慎  阅读(138)  评论(0编辑  收藏  举报