编写算法,实现带头结点单链表的逆置算法。
void ReverseList(LinkList &L){
LNode *cur=L->next;
LNode *next=NULL;
L->next=NULL;
while(cur){
next=cur->next;
cur->next=L->next;
L->next=cur;
cur=next;
}
void ReverseList(LinkList L) { LinkList p = L->next; // p指向第一个结点 LinkList r = L; // r始终指向逆置后的最后一个结点,初始时指向头结点 LinkList s; // s用于临时保存结点 while (p != NULL) { s = p->next; // 保存p的下一个结点 p->next = r; // 将p的next指向逆置后的链表的最后一个结点 r = p; // 移动r到p p = s; // 移动p到下一个结点 } L->next = r; // 将头结点的next指向新的逆置后的第一个结点 }
void NZ(Linklist &L){
Lnode *p=L->next;
Lnode *pre=NULL;
Lnode *next=NULL;
while(p){
next=p->next;
p->next=pre;
pre=p;
p=next;
L->next=pre;
void reverseLink(LinklList head){
LNode *p = head->next;
LNode *head2 = p->head;
p-next = NULL;
while(head2 != NULL){
LNode *q = head2;
head2 = head2->next;
q->next = p;
head->next = q;
将单链表就地逆置可以考虑使用头插法。
LinkList Reverse(LinkList L){ LNode *p = L->next; LNode *r; L->next = NULL; while(p){ r = p->next; p->next = L->next; L->next = p; p = r; } return L; }
LinkList* reverse(LinkList& L) { if(L->next == NULL) return L; LinkNode *p = L->next, *q; p = p->next; while(p != NULL) { q = p->next; p->next = L->next; L->next = p; p = q; } return L; }
public class headinsert{
Node newnode=new node(o)
btnode *revers(btnode *h){
btnode *rear=(btnode*)malloc(sizeof(btnode));
btnode p;
rear->next=NULL;
rear->next=h->next;
h->next=h->next->next;
rear->next->nexet=NULL
p=h->next;
while(h->next){
h->next=p->next;
p->next=rear->next;
rear->next=p;
free(h);
return rear;
void reverse(LinkList &L){
LNode *p = L->head, *head = L->head, *q = p->next;
while(!p){
p = q;
q = q->next;
p->next = head->next;
head->next = p;
void Reverse(LinkList *L){
if(!L -> next) return ERROR;
LinkList *p = L -> next;
LinkList *q = p -> next;
L -> next == NULL;
p -> next = L-> next;
L -> next = p;
q = q -> next;
return L;
ListNode* reverseList(ListNode* head){
if(!head || !head->next) returnhead;
auto new_head = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return new_head;
void invert(Lnode *head) { Lnode *p, *q; p -> next = head -> next, q -> next = p -> next; while(q) { p = q, q = q -> next; p -> next = head -> next; head -> next = p; } }
Inverse(Linklist L){ Linklist q; //头插法建立的新链表 LNode* p = L->next; //在旧链表上摘结点 q->next = NULL; while(p!=NULL){ L->next = p->next; p->next = q->next; q->next = p; p = L->next; } }
Typedef struct LNode{ ElemType data; struct LNode *next ; } LNode,*LinkList; void invert (LinkList& L){ #头指针法 LNode *p,*r; P=L → next; l=next=null; While ( p ! =null) { r =p → next; P - next= i c → next; }
void reverseList(LNode *L){
LNode *pL->next,*q;
q=p->next
p->next=L->next;
L->next=p;
p=q;
头插发建立单链表
void invent(Lnode *head)
{Lnode *p,*q;
if(!head->next) return ERROR;
p=head->next; q=p->next; p->next =NULL;
while(q)
{p=q; q=q->next; p->next=head->next; head->next=p;}
访问->头插法
头插法
if(head->next==NULL||head->next->next==NULL) return;
node *p=head->next;
node *q=head->next->next;
while(p!=NULL)
{
if(head->next==p)p->next=NULL;
else p->next=head->next;
head->next=p;
if(q!=NULL)q=q->next;
vg
#include<stdio.h>
typedef struct link {
int data;
struct link *next;
} link;
//带头结点
link *initlinklist(int a[],int size) {
link *head=(link*)malloc(sizeof(link));
head->next=NULL;
link *p=head;
for(int i=0; i<size; i++) {
link *temp=(link*)malloc(sizeof(link));
temp->data=a[i];
temp->next=NULL;
p->next=temp;
p=p->next;
return head;
void display(link *head) {
link *temp=head->next;
while(temp!=NULL) {
printf("%d",temp->data);
if(temp->next)
printf("->");
temp=temp->next;
printf("\n");
//逆置算法--就地逆置法
link *reverselinklist(link *head) {
link *beg,*end,*p=head->next;
while(head==NULL||head->next==NULL) {
printf("空表\n");
beg=p;
end=p->next;
while(end!=NULL) {
beg->next=end->next;
end->next=p;
p=end;
end=beg->next;
int main()
int a[]= {1,2,3,4,5,6,7,8,9};
int size=sizeof(a)/sizeof(a[0]);
link *head=initlinklist(a,size);
display(head);
link *p=reverselinklist(head);
display(p);
return 0;
typedef struct LNode{
struct LNode *next;
}LNode,*LinkList;
void Reverse(LinkList &L){
LNode *p=L->next,*r;
while(p!=NULL){
r=p->next;
p=r;
void invent(Lnode *head){
Lnode *p,*q;
if(!head->next)
return ERROR;
p=head->next;
q=p->next;
while(q){
p->next=head->next;
q=q->next;
#include<iostream> using namespace std; struct Lnode{ int data; Lnode *next; }; void reverse(Lnode * l){ Lnode * p,*q; p=l->next->next; while(!p){ q = p; p=p->next; q->next = l->next; l->next = q; } }
void revert(LinkList *L){
LinkList *begin,*end;
begin = L->next;
end = L ->next->next;
while(end !=null){
begin->next = end->next;
end->next = L ->next;
L->next = end;
end = begin->next;
Linklist reverse(LinkList L){
LNode *p,*r;
p=L->next;
:
// 带头结点单链表的逆置算法
void ReverseList(LinkList L) {
if (L == NULL || L->next == NULL)
return;
LNode *prev = NULL;
LNode *current = L->next;
LNode *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next; }
L->next = prev;
void reverse(LinkList L){
Node *head = L->next;
Node *old = NULL, *tmp;
while(head != NULL){
tmp = head->next;
head->next = old;
old = head;
head = tmp;
void invent(Lnode *L){
if (!L->next) return ERROR;
p=L->next;q=p->next;p->next=NULL;
while (q)
{p=q;q=q->next;p->next=L->next;L->next=p;}
if(!head->next) return ERROR;//头结点为空跳出
p=head->next; q=p->next; p->next =NULL;//头结点分开
p=q; q=q->next; p->next=head->next; head->next=p;//尾插法插入
//单链表逆置 void ReverseLNode(LNode *head){ printf("单链表逆置后--\n"); LNode *p=head->next; //此时p为结点1 LNode *q=p->next; //此时q为结点2 p->next=NULL; //结点1变为尾结点,指向赋空 while (q){ //如果后续结点不为空 p=q;//p变为结点2 q=q->next; //q变为结点3 p->next=head->next; //结点2指向结点1 head->next=p; //头结点指向结点2(因为此时的结点1变为了尾结点,而原先的结点2变为了首结点。 } }
bool re_linklist(LinkList &L){
LNode *p,*q;
if(L->next==NULL) return false;//单链表为空
if(p->next==NULL) reurn true;//单链表只有一个节点,逆置等于它本身
if(q!=NULL) q=q->next;
return true;
void reverse (Linklist &L)
Lnode *p=l->next,*r;
l-next=null;
while(p!=null)
r=l->next;
l-next=p;
//不知道用递归对不对
LinkNode* Reverse(LinkNode* head)
if(head->next==NULL) return NULL;
LinkNode* temp=head->next;
head->next=Reverse(LinkNode* head->next->next);
Reverse(LinkNode* head->next->next) = temp;
return NULL;
fzh 回复 fzh: LinkNode* Reverse(LinkNode* head) { if(head->next==NULL) return NULL; LinkNode* temp=head->next; head->next=Reverse(head->next->next); Reverse( head->next->next) = temp; return NULL; }
ListNode* reverseList(struct ListNode* head){
ListNode *prev=NULL;
ListNode *curr=head;
while(curr){
//由于倒转指针指向后会断开连接,所以用临时结点temp保存curr指向的下一结点
ListNode *temp = curr->next;
//链表指针方向倒转
curr->next=prev;
//两指针依次向后移动
prev=curr;
curr=temp;
return prev;
代码怎么学 一窍不通 有没有大佬给点建议
月溅星河 回复 椰噜: 新手建议看C语言快速入门:https://noobdream.com/Major/majorinfo/1/
头插法实现单链表逆置
关键点:
1构建带头结点的空链表 R
2 遍历原链表,将每个结点以头插法插入到R中。
3 返回R
void Reverse_List(LinkList L) { LNode* r=L; LNode* p=L->next; L->next=NULL;//记得把头指针next域置空,否则会循环指首结点 while(p) { r=p->next; p->next=L->next; L->next=p; p=r; } }
//头插法逆置 LinkList Rverse_1(LinkList L){ LNode *p,*r;//p为工作指针,r为p的后继,防止断链 p = L->next;//从第一个结点开始 L->next = NULL;//先将头结点L的next置为NULL while(p!= NULL){//依次拿下结点 r = p->next;//暂存p的后继 p->next = L->next;//将p插入在头结点之后 L->next = p; p = r; } } //改变指针指向 LinkList Rverse_1(LinkList L){ LNode *pre,*p = L->next,*r = p->next; p->next = NULL; while(r!= NULL){ pre = p; p = r; r = r->next; p->next = pre; } L->next = p; return L; }
mk921 回复 兜兜里没有糖: 三指针
LinkList reverse(LinkList L){ LinkList P=L->next,r; L->next=null; while(p){ r=p->next; p->next=L->next; L->next=p; p=r; } return L; }
void Revert(Sqlist &L)
Sqlist *p,*q;
if(L->next == NULL) return;
p = L->next;
q = p ->next;
p -> next = NULL;
p ->next = L->next;
L->next = p;
if(L-next==NULL)//判断传入链表是否为空
{ return flase;}
p=L->next;//将待处链表传递给p
q=p->next;//第二结点开始遍历
p->next=NULL;//设置一个新链表
while(q){
p=q;//结点传递
q=q->next;//进行L链表的位置移动
p->next=L->next;//头插法
void invent(Lnode *head) {Lnode *p,*q; if(!head->next) return ERROR; p=head->next; q=p->next; p->next =NULL; while(q) {p=q; q=q->next; p->next=head->next; head->next=p;} }
思路
1.初始化2个指针,previous(前一个结点),current(当前结点)。previous指向NULL,current指向头结点L。
2.判断条件当L->next = NULL时返回,不需逆置。
3.保存current->next结点nextNode,使current指向previous,将previous移到current位置,current指向nextNode位置。重复23步骤。
void reverse(struct LNode* L) { if(L->next = NULL); { return FALSE; } struct LNode* current = L; struct LNode* previous = NULL; while (L->next != NULL) { nextNode = current->next; current->next = previous; previous = current; current = nextNode; }//最后previous指向反转后的头结点。 return L; }
不知
void invert(Lnode *head){
Lnode *p, *q;
if(!head->next){
p = head->next;
q = p->next;
p->next = NULL;
void reverse(LinkList *L)
LinkNode * p = L ->next;
LinkNode * q = p;
L->next = NULL;
while(p)
p->next = L->next;
void reverse(NodeList L) { LNode* p = new LNode; LNode* q = new LNode; p = L->next; L->next = NULL; while (p!=NULL) { q = p->next; p->next = L->next; L->next = p; p = q; } }
void reverseLinkedList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return; } struct ListNode* prev = NULL; struct ListNode* curr = head->next; while (curr != NULL) { struct ListNode* nextNode = curr->next; curr->next = prev; prev = curr; curr = nextNode; } head->next = prev; }
Node *head_reverse(Node *head) { // 头插法实现元素逆置 Node *p, *q; p = head->next; // p指向第一个有元素的节点 head->next = NULL; // 将head分离出来 while (p != NULL) { q = p->next; // q指向p的下一个节点 p->next = head->next; // 头插法把q插入head中 head->next = p; p = q; } return head; }
LinkList reverse(LinkList L){
ListNode *pre,*p=L->next,*r=p->next;
p->next=NULL;
while(r!=NULL){
r=r->next;
L=p->next;
LinkList Reverse_2(LinkList L){ //依次遍历线性表,并将结点指针反转 LNode *pre, *p=L->next, *r=p->next; p->next = NULL; //处理第一个结点 链表反转后,第一个结点变为最后一个结点,其next域为NULL while(r!= NULL){ /* 后三句依次更新指针,遍历单链表*/ pre = p; p = r; r = r->next; p->next = pre; //反转指针 } L->next = p; //处理最后一个结点,链表反转后,最后一个结点变为第一个结点,L->next要指向其 return L; }
1. p=L-›next; L-›next=NULL; while (p){ r=p-›next; p-›next=L-›next;//断p-r p=r;} 2. p=L-›next; r=p-›next; p-›next=NULL;//断p-r while(r){ pre=p; p=r; r=r-›next;//依次后移 p-›next=pre;//断p-r L-›next=p;}
void reverse(TreeNode * head)
TreeNode *p = head->next, *last = NULL;
while (p)
TreeNode *s = p->next;
p->next = last;
last = p, p = s;
head->next = last;
void reverse(List *head)
List *p,*q;
print(head);
#include<stdio.h> #include<stdlib.h> typedef struct node{ int data; node *next; }List;
List* create(){ //创建单链表 List *head = (List*)malloc(sizeof(List)); head->next=NULL; List *tail=head; int i; for(i=0;i<10;i++){ List *p = (List*)malloc(sizeof(List)); p->data=i; p->next=tail->next; tail->next=p; tail=p; } return head; } void reverse(List *head){ //逆置单链表 List *p,*q; p=head->next; q = p->next; head->next=NULL; while(p!=NULL){ p->next=head->next; head->next=p; p=q; if(p==NULL) continue; q=p->next; } } void print(List* head){ //打印单链表 head=head->next; while(head!=NULL){ printf("%d ",head->data); head=head->next; } printf("\n"); }
int main(){ List* head = create(); print(head); reverse(head); print(head); return 0; }
//链表逆置 void Inverse(LinkList L) { LNode* p; LNode* q; p = L->next; L->next = NULL; while (p) { q = p->next; p->next = L->next; L->next = p; p = q; } }
`aa`
void reversed_hh(LinkList L1){ LNode *p = L1->next; LNode *q = p->next; p->next = NULL; while (q ->next != NULL){ LNode *t = q->next; q->next = p; p = q; q = t; } q->next = p; L1->next = q; }
//带头结点单链表逆置算法(逆置:可以从后往前遍历,也可以直接接原有后面)
void reverse_head(Lnode &L)
{ Lnode *p,*q;
{ p=L->next;
p->next=null;
{ for(i=0;i<n;i++)
{ p=q;
Lr-next = NULL;
void Reverse(LinkList &L){ LNode *p=L->next,*r; L->next=NULL; while(p!=NULL){ r=p->next; p->next=L->next; L->next=p; p=r; } }
int fun(List &L) { Node *p = L; int n = 0; while (p != NULL) { p = p->next; n++; } for (int i = 0; i < n; i++) { p->next = head->next; head->next = head->next->next; p->next->next = NULL; p = p->next; } return true; }
LNode reverse(LNode *head){
head->next=null;
while(p!=null){
q=p;
//头插法
q->next = head->next;
LinkList reverse(LinkList L){ Lnode *p, *q; p = L->next; L->next = NULL; while(p!NULL){ q = p->next; p->next = L->next; L-next = p; p = q; } return L; }
LinkList Reverse_1(LinkList L){
L->next=NULL;
void reverse(LNode head){
LNode p*,q*;
p = head->next; q = p -> next; p -> next = null;
while(!q){
p = q; p->next = head->next;head->next = q; } }
wyh917917 回复 wyh917917: while(q)
void reserve(Linklist L){
if(NULL==L->next){ return EOF;}
if(L->next!=NULL){
reserve(L->next);
if(L!=NULL) {
printf(L->data);
答案:void invent(Ln...
用户登录可进行刷题及查看答案
答案:void invent(Lnode *head)
登录后提交答案