/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *p,*q;
//p指向头结点的下一个结点
p=pHead->next;
//将链表断开,头结点置为空
pHead->next = NULL;
//p不等于空向后遍历,直到找到最后一个结点
while(p != NULL)
{
//q指向p后面的一个结点
q = p->next;
//保护pHead后面的结点
p->next = pHead->next;
//将p链接到pHead后面
pHead->next = p;
//将p指针向后挪动
p = q;
}
return pHead;
}
};
双向链表交换相邻结点:
#include <stdio.h>
struct node
{
int data;
node *pPre; //前驱
node *pNext;//后继
}
void swap(node *p,node *q)
{
node *(p-1); //p结点的前一个结点
node *(q+1); //q结点后面的一个结点
p-1->pNext = q; //p-1结点指向q结点
q->pPre = p->pPre; //将p的前驱赋给q
q+1->pPre = p; //q+1结点指向p结点
p->pNext = q->pNext; //q的后继值赋给p
//q指向p
q->pNext = p;
//p指向q
p->pPre = q;
}
int main()
{
return 0;
}