反转单向链表

  数组生成单向链表

const createLinkList = (array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) => {
  let root = null;
  for (let index = 0; index < array.length; index++) {
    const element = array[index];
    const node = {
      value: element,
      next: null,
    };
    node.next = root;
    root = node;
  }
  return root;
};

  反转单向链表

const reserveLinkList = (root = createLinkList()) => {
  let linklist = null;
  while (root) {
    const node = {
      value: root.value,
      next: null,
    };
    node.next = linklist;
    linklist = node;
    root = root.next;
  }
  return linklist;
};

  

 

posted @ 2023-01-26 16:55  671_MrSix  阅读(9)  评论(0编辑  收藏  举报