【刷算法】翻转单链表的递归和非递归方法
题目描述
输入一个链表,反转链表后,输出新链表的表头。
分析
典型的面试题以及大学数据结构课程常见题,没啥好分析的了...
代码实现
递归版
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; }
相关推荐
steeven 2020-11-10
Tips 2020-10-14
nongfusanquan0 2020-08-18
yedaoxiaodi 2020-07-26
清溪算法君老号 2020-06-27
pengkingli 2020-06-25
yishujixiaoxiao 2020-06-25
清溪算法 2020-06-21
RememberMePlease 2020-06-17
nurvnurv 2020-06-05
SystemArchitect 2020-06-02
码墨 2020-05-29
清溪算法 2020-05-27
choupiaoyi 2020-05-27
清溪算法 2020-05-25
bluewelkin 2020-05-19
dbhllnr 2020-05-15
steeven 2020-05-09
baike 2020-05-09