// 1.单链表逆序//链表为带头结点的,head指向第一个结点typedef struct List{ int data; List * next; }NodeList; NodeList * Reverse(NodeList * head){ if (!head || !head->next) return head; NodeList *p1 = head; NodeList *p2 = head->next; head->next = NULL; while(p2) { p1= p2; p2 = p2->next; p1->next = head; head=p1; } return head;}// 2.下面的例子为 “单链表逆序输出”void printList (NodeList *head){ if (head == NULL) return; if (head->next == NULL) printf("%d", head->data); else { printList (head->next); printf("%d", head->data); }}
单链表逆序
最新推荐文章于 2024-07-25 12:00:37 发布
// 1.单链表逆序//链表为带头结点的,head指向第一个结点typedef struct List{ int data; List * next; }NodeList; NodeList * Reverse(NodeList * head){ if (!head || !head->next) return head; NodeList *p1 = head; NodeList *p2 = head->next; head->next = NULL; while(p2) { p1= p2; p2 = p2->next; p1->next = head; head=p1; } return head;}// 2.下面的例子为 “单链表逆序输出”void printList (NodeList *head){ if (head == NULL) return; if (head->next == NULL) printf("%d", head->data); else { printList (head->next); printf("%d", head->data); }}