博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linked List Cycle II
阅读量:4494 次
发布时间:2019-06-08

本文共 2448 字,大约阅读时间需要 8 分钟。

1. Title

Linked List Cycle II

2. Http address

https://leetcode.com/problems/linked-list-cycle-ii/

3. The question

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up:

Can you solve it without using extra space?

 4. My code(AC)

  • 1  // Accepted 2        public static ListNode detectCycle(ListNode head) { 3             4            if( head == null) 5                return null; 6            ListNode last ,pre; 7            last = head; 8            pre = head.next; 9            10            if( pre == last)11                return head;12            while( pre != null)13            {14                if( pre == last)15                {16                    break;17                }18                last = last.next;19                pre = pre.next;20                if( pre != null)21                {22                    pre = pre.next;23                }else{24                    pre = null;25                }26            }27            28            if ( pre == null)29            {30                return null;31            }32            ListNode second = pre.next;33            ListNode first = head;34            pre.next = null;35            36            int lenA,lenB;37           38            ListNode p = first;39            lenA = 0 ;40            while( p != null)41            {42                lenA++;43                p = p.next;44            }45            lenB = 0 ;46            p = second;47            while( p != null)48            {49                lenB++;50                p = p.next;51            }52            53            if( lenA >= lenB)54            {55                int dis = lenA - lenB;56                while( dis > 0 && first != null)57                {58                    first = first.next;59                    dis--;60                }61                62            }else{63                int dis = lenB - lenA;64                while( dis > 0 && second != null)65                {66                    second = second.next;67                    dis--;68                }69            }70            71            while( first != null && second != null && first != second)72            {73                first = first.next;74                second = second.next;75            }76            return second;77            78          79         }

     

 

 
 

转载于:https://www.cnblogs.com/ordili/p/4970027.html

你可能感兴趣的文章
Struts2学习笔记(十二) 类型转换(Type Conversion)(下)
查看>>
tcpdump学习
查看>>
局域网内传输文件速度慢
查看>>
Linux的核心版本(摘抄)
查看>>
CASE表达式
查看>>
zkw线段树
查看>>
作业1226
查看>>
mainline.js主线
查看>>
fseek()
查看>>
Python学习笔记——PyQt控件中文字居中显示
查看>>
JAVA环境下利用solrj二次开发SOlR搜索的环境部署常见错误
查看>>
Beta阶段敏捷冲刺前准备
查看>>
mini web框架-3-替换模板
查看>>
Siamese Network简介
查看>>
svg学习(三)rect
查看>>
ruby 模块 的引入
查看>>
CI Weekly #21 | iOS 持续集成快速入门指南
查看>>
Jquery获取输入框属性file,ajax传输后端,下载图片
查看>>
docker运行环境安装-后续步骤(二)
查看>>
Python学习——02-Python基础——【3集合与函数】
查看>>