[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 *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

/**
 * Reverse Iteratively
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* revHead = NULL;
        ListNode* rev = head;

        while(rev){
            head = rev;
            rev = rev->next;
            head->next = revHead;
            revHead = head;
        }

        return revHead;

    }
};

用递归来做:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

/**
 * Reverse Iteratively
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {

        if(!head || (!head->next))return head;
        ListNode* node = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return node;

    }
};
时间: 2024-10-13 22:20:33

[LeetCode]206. Reverse Linked List 解题小结的相关文章

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. #

【LeetCode】206 - Reverse Linked List

Reverse a singly linked list. Hint:A linked list can be reversed either iteratively or recursively. Could you implement both? Solution 1:iteration 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *

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

[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]Add Two Numbers

题目 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&g

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] 237. Delete Node in a Linked Lis解题小结

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -