206. 反转链表

cpp 复制代码
/**
 * 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==NULL)return NULL;
        ListNode *cur=head;
        ListNode *p=NULL;
        if(cur->next==NULL)return head;
        while(cur){
            ListNode *tem=cur->next;
            cur->next=p;
            p=cur;
            cur=tem;
        }
        return p;
    }
};
相关推荐
轴测君2 小时前
3 无重复字符的最长子串
数据结构·算法·leetcode
lifallen12 小时前
Hadoop MapReduce 任务/输入数据 分片 InputSplit 解析
大数据·数据结构·hadoop·分布式·算法
熙xi.12 小时前
数据结构 -- 哈希表和内核链表
数据结构·算法·散列表
Univin14 小时前
8.25作业
数据结构·windows
胡萝卜3.016 小时前
数据结构初阶:详解单链表(一)
数据结构·笔记·学习·单链表
闪电麦坤9517 小时前
数据结构:红黑树(Red-Black Tree)
数据结构··红黑树
墨染点香18 小时前
LeetCode 刷题【53. 最大子数组和】
数据结构·算法·leetcode
NekoCNN19 小时前
现代视角下的线性表全解
数据结构
工藤新一¹19 小时前
C/C++ 数据结构 —— 树(2)
c语言·数据结构·c++·二叉树··c/c++