zoukankan      html  css  js  c++  java
  • 面试题-链表反转c实现

    // ListReverse.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <malloc.h>
    #include <iostream>
    using namespace std;

    typedef struct List
    {
        struct List *next;
        int data;    
    }*ListPtr;


    void PrintList(ListPtr list)
    {
        while(list!=NULL)
        {
            cout<<list->data<<"->";
            list = list->next;
        }
        cout<<endl;
    }

    ListPtr ReverseList(ListPtr list)
    {
        if((list == NULL) || (list->next ==NULL))
        {
            return list;
        }

        ListPtr head = list;
        ListPtr headnext = head->next;
        ListPtr headnextnext = headnext->next;
        head->next = NULL;
        
        while(headnextnext != NULL)
        {
            headnext->next = head;
            head = headnext;
            headnext = headnextnext;
            headnextnext = headnextnext->next;    
        }
        headnext->next = head;
        return headnext;

    }


    int _tmain(int argc, _TCHAR* argv[])
    {
        ListPtr head = (ListPtr)malloc(sizeof(List));
        ListPtr headtemp = head;
        int i=10;
        while(i--)
        {
            headtemp->data = i;
            headtemp = headtemp->next  =  (ListPtr)malloc(sizeof(List));

        }
        headtemp->data = i;
        headtemp->next = NULL;

        PrintList(head);    
        PrintList(ReverseList(head));
        


        return 0;
    }

  • 相关阅读:
    Radmin01-如何使用Radmin进行云服务器的远程连接与文件传输?
    vmware-workstation迁移虚拟机 15pro到12版本
    爬格子呀5-10
    爬格子呀--IEEE极限编程大赛留念
    爬格子呀5-8
    爬格子呀5-7
    爬格子呀5-6
    爬格子呀5-5
    爬格子呀5-4
    爬格子呀5-3
  • 原文地址:https://www.cnblogs.com/harlanc/p/5162202.html
Copyright © 2011-2022 走看看