/** * 单链表的倒数第k个节点 */ const linkList = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5, next: { value: 6, next: { value: 7, next: { value: 8, next: { value: 9, next: { value: 10, next: null } } } } } } } } } } const nthValueFromEnd = (node = linkList, k = 3) => { let link1 = node for(let i = 0; i < k; i++){ link1 = link1.next } let link2 = node while(link1.next){ link1 = link1.next link2 = link2.next } return link2.value }
标签:node,单链,link1,link2,value,next,let,节点,倒数第 From: https://www.cnblogs.com/zhenjianyu/p/17077199.html