相关题目:
反转链表https://www.acwing.com/problem/content/description/33/
解题思路:
画图加上头插法可以解决该问题。
相关代码:
/**
* 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) {
ListNode *Head =new ListNode(-1); //定义一个头节点
Head->next=head;
ListNode *p=Head->next;
ListNode *q;
Head->next=NULL;
while(p != NULL){
q=p->next;
p->next=NULL;
p->next = Head->next;
Head->next=p;
p=q;
}
return Head->next;
}
};