小程序 - 链表检测环/链表是否交叉 等
2016-03-31 00:03
525 查看
链表检测环
链表是否交叉
两个链表第一个公共节点
int cycleExists(Node *head) { Node *fast, *slow; if (head == NULL) return 0; for (slow = head, fast = head->next; fast && fast->next; fast = fast->next->next, slow = slow->next) if (slow == fast) return 1; return 0; }
链表是否交叉
int listOverlapped(Node *list1, Node *list2) { if (!list1 || !list2) return 0; for (; list1->next; list1 = list1->next); for (; list2->next; list2 = list2->next); return list1 == list2; }
两个链表第一个公共节点
Node* firstCommonNode(Node *list1, Node *list2) { int num1, num2; Node *n1, *n2; if (!list1 || !list2) return NULL; for (num1 = 0, n1 = list1; n1->next; num1++, n1 = n1->next); for (num2 = 0, n2 = list2; n2->next; num2++, n2 = n2->next); if (n1 != n2) return NULL; if (num1 > num2) for (num1 -= num2; num1 > 0; num1--, list1 = list1->next); else for (num2 -= num1; num2 > 0; num2--, list2 = list2->next); for (; list1 != list2; list1 = list1->next, list2 = list2->next); return list1; }
相关文章推荐
- 实现微信好友列表的php代码
- 微信公众平台开发入门教程(PHP从入门到精通)
- Qt实现记事本的打开和保存功能(第一个实用的Qt小程序)
- Android开发:实现分享给微信好友以及出现闪退的问题
- Android小程序-Walker登录页面(一)
- 微信手机网页上传图片高效率压缩(Canvas+Base64)
- #小河学习笔记#前端精品视频教程:微信场景开发实战
- 微信Token验证代码的实现
- 微信支付v3开发(5) 扫码并输入金额支付
- 微信支付v3开发(5) 扫码并输入金额支付
- 微信支付v3开发(6) 收货地址共享接口
- 微信支付v3开发(6) 收货地址共享接口
- 浅谈《微信抢红包原理》
- 检测是否是微信浏览器
- 使用delphi+intraweb进行微信开发1~4代码示例
- 一步一步实现iOS微信自动抢红包(非越狱)
- 微信公众号开发
- javascript 微信企业号APP应用URL转换成PC端可正常访问的URL
- android批量上传图片(模仿QQ空间和微信发表说说)
- 判断是否是微信浏览器