我正在尝试使用堆栈来反转两个给定索引im之间的链表中的节点,以执行此操作以保存所有相关节点,但是我无法访问该节点,从而在此特定行上遇到运行时错误
p.first-> next = s.top();
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
using namespace std;
#include <stack>
#include <pair>
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* nd = head;
int count = 0;
stack<ListNode*> s;
pair<ListNode*, ListNode*> p;
while(nd->next != NULL){
count ++;
if(count = m-1){
p.first = nd;
}
if(count >= m and count <= n){
s.push(nd);
}
if(count = n+1){
p.second = nd;
}
nd=nd->next;
}
(p.first)->next = s.top()
ListNode* temp =s.top();
s.pop();
/*
while(!s.empty()){
temp->next = s.top();
s.pop();
temp = temp->next;
}
temp->next = p.second;
*/
return head;
}
};