【leetcode】Linked List Cycle II,判断链表是否有环

本题意思是无修改的判断一个链表是否具有环,并且返回环的初始节点,否则返回NULL。
一种方法是对已经访问到的节点设置一些标志,比如将next指针指向某一个特定节点,或者其他方法等。这样的问题是会对链表进行修改。另一种方法是采用两个指针在链表上滑动,一个指针每次只滑过一个节点,另一个指针每次滑过两个节点,连个指针同时从起点开始滑动。可以证明,当存在环时,两个指针会在某次相遇在同一个节点,否则,快的指针会先到达NULL。
针对本题,本文对两种方法都实现了。第一种方法最后需要将next的修改又改回原来值。而第二种方法是在判断具有环后正确找出环的初始节点。当两个指针相遇后,固定一个指针不动,另一个指针继续滑动,每次滑动一个节点并计数,当再次相遇时,可得到环具有的节点数n。然后使两个指针指向头结点,当其中一个指针先于另一个指针n个节点后同时同步(每次滑动一个节点)滑动。由于两个指针距离等于环具有的节点数n,那么他们必然在环初始节点处相遇,这样就找到了环初始节点了。
代码如下:

//using inplace marks
    ListNode* func(ListNode* head,ListNode* tag)
    {
       if(head==NULL)return NULL;
        if(head->next == tag )
        {
            return head;
        }
        ListNode *p=head->next;
        head->next = tag;
        ListNode* ret=func(p,tag);
        head->next = p;
        return ret;
    }
    ListNode *detectCycle(ListNode *head) {
        ListNode* tag = new ListNode(0);
        if(head==NULL)
        {
            return NULL;
        }
        ListNode* p = head->next;
        head->next = tag;
        ListNode* ret = func(p,tag);
        head->next = p;
        return ret;
    }
    //using fast and slow pointers
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast=head,*slow=head;
        while(fast!=NULL&&slow!=NULL)
        {
            fast = fast->next;
            if(fast == NULL) return NULL;  //if doesn't has a cycle,then fast reach the end before the slow.
            fast = fast->next;
            slow = slow->next;
            if(fast==slow) break;
        }
        if(fast!=NULL) // if has a cycle
        {
            int n=1;  // n is the  number of nodes in the cycle. 
            fast = fast->next;
            while(fast!=slow)
            {
                ++n;
                fast=fast->next;
            }
            //Then we let two pointers begin from the front,and one begins after the another n nodes, 
            //so that they can meet at the node where cycle begins.
            fast=head,slow=head;
            while(n>0)
            {
                fast = fast->next;
                --n;
            }
            while(fast!=slow)
            {
                fast=fast->next;
                slow=slow->next;
            }
            return fast;
        }
        else
        {
            return NULL;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本题要求合并两个有序链表。对于链表中的每一个节点,由于链表是有序的,所以可以将两个链表中的节点按照大小顺序进行比较,然后逐个将较小的节点链接上去,最终得到一个新的有序链表。 使用Rust语言实现这个问题,需要首先定义一个链表节点的结构体,包含node值以及next指针。然后定义一个函数来合并两个有序链表,输入为两个链表的头节点指针,输出为新的有序链表的头节点指针。 在合并过程中,首先需要判断两个链表的头节点哪一个较小,将较小的节点作为新链表的头节点,并将该节点的next指针指向递归调用合并函数的结果。递归结束条件为其中一个链表为空,则将另一个链表直接链接到新链表上。 完整代码如下: ```rust // 定义链表节点结构体 #[derive(Debug)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } // 合并两个有序链表 fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { match (l1, l2) { (None, None) => None, // 两个链表均为空 (l1, None) => l1, // 其中一个链表为空,直接返回另一个链表 (None, l2) => l2, (Some(mut l1), Some(mut l2)) => { if l1.val < l2.val { l1.next = merge_two_lists(l1.next, Some(l2)); Some(l1) } else { l2.next = merge_two_lists(Some(l1), l2.next); Some(l2) } } } } // 测试代码 fn main() { let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let l2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let merged = merge_two_lists(l1, l2); println!("{:?}", merged); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值