码迷,mamicode.com
首页 > 其他好文 > 详细

链表操作

时间:2014-07-19 20:28:12      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   art   

1链表逆序 http://blog.csdn.net/niuer09/article/details/5961004 

bubuko.com,布布扣
typedef struct tagListNode{
    int data;
    struct tagListNode* next;
}ListNode, *List;

List ReverseList(List head)  
{  
    if(head->next == NULL || head->next->next == NULL)    
    {  
       return head;   /*链表为空或只有一个元素则直接返回*/  
    }  
  
    ListNode *t = NULL,  
             *p = head->next,  
             *q = head->next->next;  
    while(q != NULL)  
    {          
      t = q->next;  
      q->next = p;  
      p = q;  
      q = t;  
    }  
  
    /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/  
    head->next->next = NULL;  /*设置链表尾*/  
    head->next = p;           /*调整链表头*/  
    return head;  
}
View Code

2判断是否有环,环的长度

http://www.cnblogs.com/xudong-bupt/p/3667729.html

bubuko.com,布布扣
///判断链表是否有环
LinkNode* judgeRing(Linklist list){
    LinkNode *fast = list;
    LinkNode *slow = list;

    if(list == NULL)
        return NULL;

    while(true){
        if(slow->next != NULL && fast->next != NULL && fast->next->next != NULL){
            slow = slow->next;
            fast = fast->next->next;
        }
        else
            return NULL;

        if(fast == slow)
            return fast;
    }
}

///获取链表环长
int getRingLength(LinkNode *meetNode){
    int RingLength=0;
    LinkNode *fast = meetNode;
    LinkNode *slow = meetNode;
    for(;;){
        fast = fast->next->next;
        slow = slow->next;
        RingLength++;
        if(fast == slow)
            break;
    }
    return RingLength;
}

///获取链表头到环连接点的长度
int getLenA(Linklist list,LinkNode *meetNode){
    int lenA=0;
    LinkNode *fast = list;
    LinkNode *slow = meetNode;
    for(;;){
        fast = fast->next;
        slow = slow->next;
        lenA++;
        if(fast == slow)
            break;
    }
    return lenA;
}
View Code

链表操作,布布扣,bubuko.com

链表操作

标签:style   blog   http   color   os   art   

原文地址:http://www.cnblogs.com/mainmonkey/p/3849565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!