【Easy】206. Reverse Linked List

    xiaoxiao2022-06-28  26

    Reverse a singly linked list.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if (head == NULL) return NULL; if (head->next == NULL) return head; ListNode* p = head->next; ListNode* h = reverseList(p); //到底后,h搭载着最后一个node,一层一层返回上来 p->next = head; head->next = NULL; return h; } };

    转载请注明原文地址: https://ju.6miu.com/read-1124178.html

    最新回复(0)