class ListNode { val : any ; next : ListNode ; param { * } val param { ListNode } next constructor ( val , next ) { this . val = val ; this . next = next ; } } class List { head : ListNode ; constructor ( arr ? ) { if ( arr ) { this . head = new ListNode ( arr [ 0 ] , null ) ; let current = this . head ; for ( let i = 1 ; i < arr . length ; i ++ ) { current . next = new ListNode ( arr [ i ] , null ) ; current = current . next ; } } else { this . head = new ListNode ( null , null ) ; } } param { Number } index return { ListNode } public find ( index ) { let current = this . head ; for ( let i = 0 ; i < index ; i ++ ) { current = current . next ; } return current ; } param { any } value param { number } index public insert ( value , index ) { let prev = this . find ( index - 1 ) ; let next = new ListNode ( value , prev . next ) ; prev . next = next ; } param { number } index public delete ( index ) { if ( index === 0 ) { this . head = this . head . next ; } else if ( this . find ( index ) === null || this . find ( index ) . val === null ) { throw Error ( '输入节点不存在' ) ; } else { let prev = this . find ( index - 1 ) ; prev . next = prev . next . next ; } } return { any [ ] } public PositiveTraverse ( ) { let arr = [ ] ; let current = this . head ; while ( current !== null ) { arr . push ( current . val ) ; current = current . next ; } return arr ; } return { any [ ] } public NegativeTraverse ( ) { let arr = [ ] ; let current = this . head ; while ( current !== null ) { arr . unshift ( current . val ) ; current = current . next ; } return arr ; } /**
     * 删除排序链表中的重复元素
     */ public DeleteDuplicates ( ) { let current = this . head ; let temp ; let toDelete ; while ( current && current . next !== null ) { temp = current . val ; if ( current . next . val === temp ) { toDelete = current . next ; current . next = toDelete . next ; } else { current = current . next ; temp = current . val ; } } } } export { ListNode , List } ;