码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode206 反转链表 两种做法(循环,递归)

时间:2019-08-03 00:17:31      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:tno   public   反转链表   递归   etc   tco   turn   rev   problem   

反转链表 leetcode206

方法1 循环
···
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode now = head;
while (now.next != null) {
ListNode temp = now.next;
now.next = now.next.next;
temp.next = head;
head = temp;
}
return head;
}···
方法2 递归
public ListNode reverseList2(ListNode head) { if (head == null || head.next == null) { return head; } ListNode newHead = reverseList2(head.next); ListNode now =newHead; while (now.next != null) { now = now.next; } now.next = head; head.next = null; return newHead; }

leetcode206 反转链表 两种做法(循环,递归)

标签:tno   public   反转链表   递归   etc   tco   turn   rev   problem   

原文地址:https://www.cnblogs.com/wmxl/p/11291754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!