java 206.反向链接列表(#1递归).java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 206.反向链接列表(#1递归).java相关的知识,希望对你有一定的参考价值。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
private ListNode helper(ListNode head, ListNode newHead) {
if (head == null) return newHead;
ListNode next = head.next;
head.next = newHead;
return helper(next, head);
}
public ListNode reverseList(ListNode head) {
return helper(head, null);
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}
以上是关于java 206.反向链接列表(#1递归).java的主要内容,如果未能解决你的问题,请参考以下文章
java 206.反向链接列表(#1递归).java
java 206.反向链接列表(#1递归).java
java 206.反向链接列表(#1递归).java
java 206.反向链接列表(#1递归).java
java 甲兵 - 08日至2019会话-206-反向链接列表
c_cpp 206.反向链接清单