zoukankan      html  css  js  c++  java
  • 4-1 单链表逆转

    本题要求实现一个函数,将给定的单链表逆转。

    函数接口定义:

    List Reverse( List L );
    

    其中List结构定义如下:

    typedef struct Node *PtrToNode;
    struct Node {
        ElementType Data; /* 存储结点数据 */
        PtrToNode   Next; /* 指向下一个结点的指针 */
    };
    typedef PtrToNode List; /* 定义单链表类型 */
    

    L是给定单链表,函数Reverse要返回被逆转后的链表。

    裁判测试程序样例:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int ElementType;
    typedef struct Node *PtrToNode;
    struct Node {
        ElementType Data;
        PtrToNode   Next;
    };
    typedef PtrToNode List;
    
    List Read(); /* 细节在此不表 */
    void Print( List L ); /* 细节在此不表 */
    
    List Reverse( List L );
    
    int main()
    {
        List L1, L2;
        L1 = Read();
        L2 = Reverse(L1);
        Print(L1);
        Print(L2);
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    5
    1 3 4 5 2
    

    输出样例:

    1
    2 5 4 3 1
    答案:
    List Reverse(List L)
    {
    	List ListNew = NULL; 
    	List ListCur = NULL;
    	List ListPre = NULL;
    	if(L == NULL)
    		goto End;
    	ListNew = L;
    	ListCur = L ->Next;
    	ListPre = L ->Next;
    	while(ListCur != NULL)
    	{
    		ListPre = ListCur;
    		ListCur = ListCur ->Next;
    		ListPre ->Next = ListNew;
    		ListNew = ListPre;
    	}
    	L ->Next = NULL;
    	L = ListNew;
    	
    End:
    	return L;
    
    };
    


  • 相关阅读:
    NodeJS优缺点
    移动端触摸相关事件touch、tap、swipe
    vscode使用技巧
    js 字符串转数字
    js 导出Excel
    <!--[if IE 9]> <![endif]-->
    js 异步请求
    关于windows串口处理
    mfc 托盘提示信息添加
    微软的麦克风处理示列代码
  • 原文地址:https://www.cnblogs.com/waitingforspring/p/5209963.html
Copyright © 2011-2022 走看看