206. Reverse Linked List

原创 2016年08月28日 20:25:33

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

递归:

	public ListNode reverseList(ListNode head) {
		if(head == null || head.next == null)return head;
		ListNode p = head.next;
		ListNode q = reverseList(p);
		head.next = null;
		p.next = head;
		return q;
	}

非递归:

	public ListNode reverseList(ListNode head) {
		if (head == null || head.next == null)
			return head;
		ListNode pre = head;
		ListNode p = head.next;
		ListNode tmp = null;
		pre.next = null;
		while (p != null) {
			tmp = p.next;
			p.next = pre;
			pre = p;
			p = tmp;
		}
		return pre;	
	}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

<LeetCode OJ> 206. Reverse Linked List

206. Reverse Linked List My Submissions Question Total Accepted: 74536 Total Submissions: 200484...

LeetCode206之Reverse Linked List Java题解

题目: Reverse a singly linked list. 解题: 采用最直接的思路,从链表的第二个节点开始向后遍历,将每一个遍历的节点插入作为当前的第一个节点,为了方便操作,我们定义一个f...

LeetCode-206. Reverse Linked List (Java)

Reverse a singly linked list. ----------------------------------------------------------------------...
  • NOadu
  • NOadu
  • 2017-06-28 10:19
  • 92

[206] Reverse Linked List

翻转一个单向链表。

【LeetCode】206. Reverse Linked List 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/pro...

leetcode微软,亚马逊,雅虎面试题206. Reverse Linked List的java实现

这是一道道简单的链表题,但是有很多大公司都有出这道面试题,可见基础题在面试中也很重要 这里有两种解法,虽然大体相同,但是耗时不同 206. Reverse Linked List Rever...

LeetCode-206&92.Reverse Linked List

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2...

[LeetCode]206. Reverse Linked List(反转单链表)

206. Reverse Linked ListReverse a singly linked list. 反向单链表。 Hint: A linked list can be reversed ...

LeetCode 206. Reverse Linked List *****三指针 (pre,cur,next)

题目 题意 注意 思路题目 Reverse a singly linked list. 题意 反转一个单链表 e.g: 1->2->3->4->5->NULL NULL<-1...

Leetcode 206. Reverse Linked List

Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursivel...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)