科帮网-Java论坛、Java社区、JavaWeb毕业设计

登录/注册
您现在的位置:论坛 资料库 JAVA开发 > LeetCode Linked List Cycle & Linked List Cycle II ...
总共48086条微博

动态微博

查看: 1298|回复: 0

LeetCode Linked List Cycle & Linked List Cycle II

[复制链接]

279

主题

41

听众

689

金钱

版主

该用户从未签到

跳转到指定楼层
楼主
发表于 2015-02-07 16:17:14 |只看该作者 |倒序浏览
Linked List Cycle
题意:给定一个链表,求这个链表有无循环
解法一:按照题目要求,不用额外空间做,我的想法是如果当前链表是a0->a1-> a2-> a3,那么不妨进行一次遍历,当前为temp,前一个为pre,后一个为next,然后将temp的next指针指向pre,这样一直遍历,如果最后跑到了head,就表示有环,否则无环。有一个bug是head是null的情况需要特判。
//  Definition for singly-linked list.
class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
                val = x;
                next = null;
        }
}

public class Solution {
        public boolean hasCycle(ListNode head) {
                if (head == null)
                        return false;
                ListNode pre = head, temp = head.next, next;
                head.next = null;
                while (temp != null) {
                        if (temp == head)
                                return true;
                        next = temp.next;
                        if (next == null)
                                return false;
                        temp.next = pre;
                        pre = temp;
                        temp = next;
                }
                return false;
        }
}
解法二:看到submission里面我的速度有些太慢了,于是百度了一下,发现一个更好的方法。两个指针,fast和slow,fast每次走两个next,slow走一个,当两个相遇的时候就表示有环。
public class Solution {
        public boolean hasCycle(ListNode head) {
                ListNode fast = head, slow = head, temp1, temp2;
                while (head != null && slow != null) {
                        slow = slow.next;
                        temp1 = fast.next;
                        if (temp1 == null)
                                return false;
                        temp2 = temp1.next;
                        if (temp2 == null)
                                return false;
                        fast = temp2;
                        if (head == slow)
                                return true;
                }
                return false;
        }
}


Linked List Cycle II 题意:同上,只不过求的是第一个循环的节点
解法:这个按照我自己的解法,拆链没法做了,因为把原有的关系都破坏了。fast和slow的这个经典方法可以,具体的解析这个人写的很好

public class Solution {

        public ListNode detectCycle(ListNode head) {
                ListNode node = hasCycle(head);
                if (node == null)
                        return null;

                ListNode temp1 = head, temp2 = node;
                while (temp1 != temp2) {
                        temp1 = temp1.next;
                        temp2 = temp2.next;
                }
                return temp1;
        }

        public ListNode hasCycle(ListNode head) {
                ListNode fast = head, slow = head, temp1, temp2;
                while (head != null && slow != null) {
                        slow = slow.next;
                        temp1 = fast.next;
                        if (temp1 == null)
                                return null;
                        temp2 = temp1.next;
                        if (temp2 == null)
                                return null;
                        fast = temp2;
                        if (fast == slow)
                                return fast;
                }
                return null;
        }
}



科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表