interface LinkList {
value:number;
next?:LinkList
}
export function createLinkList(arr:number[]):LinkList|undefined {
if(!arr.length) return
let currentNode:LinkList = {
value:arr[arr.length-1]
}
for (let i=arr.length-2;i>=0;i--){
currentNode = {
value:arr[i],
next:currentNode
}
}
return currentNode
}
// 1->2->3->4->5->6
// 6->5->4->3->2->1
export function reverseLinkList(data:LinkList|undefined):LinkList|undefined {
if(!data) return
let preNode:LinkList|undefined
let currentNode:LinkList|undefined
let nextNode:LinkList|undefined = data
while (nextNode){
if(currentNode && !preNode){
currentNode.next = undefined
}
if(preNode && currentNode) {
currentNode.next = preNode
}
preNode = currentNode
currentNode = nextNode
nextNode = nextNode.next
}
currentNode!.next = preNode
return currentNode
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)