标签:查找 next 遍历 each class list 创建 链表 head
单链表的基本结构
function Node(val,next){ this.val = val; this.next = next || null; }
function tailCreateList(aSrc){ var head = new Node(); pHead = head; aSrc.forEach((item) => { var node = new Node(item); pHead.next = node; pHead = node; }) return head; }
function headCreateList(aSrc){ var head = new Node(); pHead = head; aSrc.forEach((item) => { var node = new Node(item); node.next = pHead.next; pHead.next = node; }) return head; }
function traverse(head,fVisit){ if(head == null){ return; } var cur = head.next; while(cur){ fVisit && fVisit(cur); cur = cur.next; } }
function find(head,val){ if(head == null){ return; } var cur = head; while(cur && cur.val != val){ cur = cur.next; } return cur; }
function insert(head,val,newVal){ if(head == null){ return; } var cur = head; while(cur && cur.val != val){ cur = cur.next; } if(cur){ var newNode = new Node(newVal); newNode.next = cur.next; cur.next = newNode; } }
标签:查找 next 遍历 each class list 创建 链表 head
原文地址:http://www.cnblogs.com/mengff/p/6876010.html