reverse_list

  • 2022-12-14
  • 浏览 (487)

reverse_list.cpp 源码

//
// Created by roseduan on 2021/9/18.
// 反转单链表

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) {
        ListNode *prev = nullptr;
        while (head != nullptr) {
            ListNode *next = head->next;
            head->next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
};

你可能感兴趣的文章

add_two_numbers

add_two_numbers_ii

intersection_two_lists

0  赞