[LC] 206. Reverse Linked List

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

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

Iterative:

/**
 * 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 pre = null, nxt = null;
        while(head != null) {
            nxt = head.next;
            head.next = pre;
            pre = head;
            head = nxt;
        }
        return pre;
    }
}

Recursive:

/**
 * 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 nxt = head.next;
        ListNode newHead = reverseList(nxt);
        nxt.next = head;
        head.next = null;
        return newHead;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12114566.html

时间: 2024-08-01 03:13:26

[LC] 206. Reverse Linked List的相关文章

leetCode 206. Reverse Linked List 反转链表

206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode(int x) : val(x), next(NULL) {}  * };

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

Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表.你能否用两种方法解决这道题? 解析 设置三个节点pre.cur.next (1)每次查看cur节点是否为NULL,如果是,则结束循环,获得结果 (2)如果cur节点不是为NULL,则先设置临时变量next为cur的下一个节点 (3)让cur

206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/#/description Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Hint: The key point is to make a copy of the next link of c

[leedcode 206] Reverse Linked List

Reverse a singly linked list. /** * 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) { //需要画图,注意每个节点的起始位置

206. Reverse Linked List (List)

Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if(!head) retur

206. Reverse Linked List(LeetCode)

Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { ListNode * pre=

LeetCode笔记:206. Reverse Linked List

题目: Reverse a singly linked list. 大意: 反转一个简单链表. 思路: 题目的意思就是给出一个链表,本来是从头指向尾的,现在要你做成从尾指向头,并且返回原来的尾,现在的头.这个肯定是要用递归或者迭代来做.只要屡清楚过程,会比较绕.大体的流程就是,把下一个节点的next指向自己,一个个迭代.递归下去,最后返回最后的原来的尾节点 他山之石: 这里给出Discuss中最火的方法. 迭代实现: public ListNode reverseList(ListNode he

Java for LeetCode 206 Reverse Linked List

Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { if(head==null) return null; Stack<ListNode> stack =new Stack<ListNode>(); ListNode temp=head; while(temp!=null){ stack.push(temp); temp=temp.ne

[LeetCode]206. Reverse Linked List 解题小结

题目: 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? 用循环来做 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *nex