LeetCode 206 Reverse a singly linked list.

Reverse a singly linked list.

Hint:

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

递归的办法:

/**
 * 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) {
        if(head==null) return null;

        if(head.next==null)return head;

        ListNode p=head.next;
        ListNode n=reverseList(p);

        head.next=null;
        p.next=head;
        return n;
    }
}

非递归,迭代的办法:

if(head == null || head.next == null)
     return head;
ListNode current =  head.next;
head.next = null;
while(current ! = null){
     ListNode temp = current.next;
     current.next = head;
     head = current;
    current = temp.next;
}
return head;
时间: 2024-10-11 08:18:03

LeetCode 206 Reverse a singly 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) {}  * };

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

Java [Leetcode 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 reverseLi

(Java) LeetCode 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? 原文地址:https://www.cnblogs.com/

LeetCode 206. Reverse Linked List(C++)

题目: 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? 分析: 分别用迭代和递归来实现. 迭代就是新建一个

LeetCode 206 Reverse Linked List 解题报告

题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 题目分析及思路 给定一个单链表,要求得到它的逆序.可以使用列表对链表结点进行保存,之后新建一个列表对链表的逆序进行保存.最后返回新建列表的第一个元素即可. python代码 # Definition for singly-linked list. #

Reverse a singly linked list

Reverse a singly linked list. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode*

[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

Leetcode 206 Reverse Linked List 链表

将单向链表反转 完成如图操作,依次进行即可 1 2 3 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* h