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.

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None

class Solution:

def reverseList(self, head: ListNode) -> ListNode:

l = [head]

if head == None or head.next == None:

return head

while l[-1].next:

l.append(l[-1].next)

ans = [l.pop()]

while l:

ans[-1].next = l.pop()

ans.append(ans[-1].next)

ans[-1].next = None

return ans[0]

原文地址:https://www.cnblogs.com/yao1996/p/10625318.html

时间: 2024-10-13 11:33:26

LeetCode 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) {}  * };

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

Reverse Linked List解题报告

Reverse Linked List 题目大意:把当前的linked list顺序颠倒 思路: 1. a,b交换值 a=temp temp = b b = a 2.用一个while循环,不断把当前拿到的值放在新的linked list的头上 3.注意循环结束条件和指针的变化 代码: 1 public ListNode reverse(ListNode head) { 2 if (head == null) { 3 return head; 4 } 5 ListNode current = ne

[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 解题小结

题目: 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

(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: Evaluate Reverse Polish Notation 解题报告

Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples:  ["2", "1", "+",