js 链表反转

/* 
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/

    function reverse(l){
        let v = ''
        while(l){
            v += l.val;
            l = l.next
        }
        return v.split('').reduce((p,c)=>{
            c = new ListNode(+c)
            c.next = p
            return c
        },null)
    }
posted @ 2020-10-04 13:12  it-pupil  阅读(670)  评论(0编辑  收藏  举报