单链表创建方法主要有头插法和尾插法,这里记录尾插法创建带头结点的单链表过程
尾插法算法代码:
LinkList List_TailInsert(LinkList &L){
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s, *r = L; // r为表尾指针
scanf("%d", &x);
while(x != 9999){
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
反转链表C语言伪代码:
struct ListNode* ReverseList(struct ListNode* pHead) {
struct ListNode* L;
L = (struct ListNode*)malloc(sizeof(struct ListNode));
L->next = NULL;
struct ListNode* p = pHead;
struct ListNode* r;
while(p != NULL){
r = p->next;
p->next = L->next;
L->next = p;
p = r;
}
return L->next;
}