輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。面試
典型的面試題以及大學數據結構課程常見題,沒啥好分析的了...數據結構
function ListNode(x){ this.val = x; this.next = null; } function ReverseList(h) { if(h === null || h.next === null) return h; var reversedHead = ReverseList(h.next); h.next.next = h; h.next = null; return reversedHead; }
function ListNode(x){ this.val = x; this.next = null; } function ReverseList(h) { if(h === null || h.next === null) return h; var pre = null; var cur = h; while(cur !== null) { var next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; }