zoukankan      html  css  js  c++  java
  • 反转链表 --剑指offer

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反正后链表的头结点。

    #include<stdio.h>
    #include<malloc.h>
    
    typedef struct node
    {
        int Element;
        struct node *Link;
    }Node;
    
    
    Node *ReverseList(Node *first)
    {
        Node *NewFirst,*p;
        if(first==NULL||first->Link==NULL)
        {
            return first;
        }
        NewFirst=first;
        while(first->Link!=NULL)
        {
            p=first->Link;
            first->Link=p->Link;
            p->Link=NewFirst;
            NewFirst=p;
        }
        return NewFirst;
    
    }
    
    void main()
    {    
        int i=0;
        Node *p,*q,*first=NULL;
        while(i<10)
        {
            p=(Node*)malloc(sizeof(Node));
            p->Element=i;
            if(!first)
            {
                first=p;
                first->Link=NULL;
            }
            else
            {
                p->Link=first;
                first=p;
            }
            i++;
        }
        q=first;
        while(q)
        {
            printf("%d,",q->Element);
            q=q->Link;
            
        }
        printf("
    ");
    
        p=ReverseList(first);
        while(p!=NULL)
        {
            printf("%d,",p->Element);
            p=p->Link;
        }
        
    }
  • 相关阅读:
    [LeetCode] 240
    [LeetCode] 169
    [LeetCode] 28
    [LeetCode] 27
    [LeetCode] 14
    [LeetCode] 9
    [LeetCode] 7
    [LeetCode] 2
    数据库开发规范
    Mysql优化
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/3935077.html
Copyright © 2011-2022 走看看