• 206. 反转链表


     

    给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

    示例 1:

    输入:head = [1,2,3,4,5]
    输出:[5,4,3,2,1]
    

    示例 2:

    输入:head = [1,2]
    输出:[2,1]
    

    示例 3:

    输入:head = []
    输出:[]
    
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode() : val(0), next(nullptr) {}
     *     ListNode(int x) : val(x), next(nullptr) {}
     *     ListNode(int x, ListNode *next) : val(x), next(next) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(head == nullptr || head->next == nullptr) return head;
            ListNode* pre = nullptr;
            ListNode* cur  = head;
            while(cur!=nullptr) {
                ListNode * next = cur->next;
                cur->next = pre;
                pre = cur;
                cur = next;
            }
            return pre;
        }
    };
  • 相关阅读:
    未解决的
    nodejs 7 和 8 的比较
    openresty Nginx
    Vim快捷键分类
    wireshark 包过滤
    RSA 公私钥 互换问题
    vim命令
    Windows 小端存储
    python 字符转换
    ssl证书验证
  • 原文地址:https://www.cnblogs.com/zle1992/p/15864921.html
Copyright © 2020-2023  润新知