标签:style blog http color os art
1链表逆序 http://blog.csdn.net/niuer09/article/details/5961004
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; }
2判断是否有环,环的长度
http://www.cnblogs.com/xudong-bupt/p/3667729.html
///判断链表是否有环 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; }
标签:style blog http color os art
原文地址:http://www.cnblogs.com/mainmonkey/p/3849565.html