2022.03.16(LC_160_相交链表)

9 篇文章 0 订阅
9 篇文章 0 订阅

方法一:哈希表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> set = new HashSet<>();
        ListNode pA = headA;
        while (pA != null) {
            set.add(pA);
            pA = pA.next;
        }
        ListNode pB = headB;
        while (pB != null) {
            if (set.contains(pB)) return pB;
            pB = pB.next;
        }
        return null;
    }
}

方法二:先让两个链表剩余长度相同,然后同步遍历

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA = getLen(headA);
        int lenB = getLen(headB);
        ListNode pA = headA;
        ListNode pB = headB;
        if (lenA > lenB) {
            for (int i = 0; i < lenA - lenB; i++) {
                pA = pA.next;
            }
        } else {
            for (int i = 0; i < lenB - lenA; i++) {
                pB = pB.next;
            }
        }
        while (pA != pB) {
            pA = pA.next;
            pB = pB.next;
        }
        return pA;
    }
    public int getLen(ListNode head) {
        int len = 0;
        while (head != null) {
            len++;
            head = head.next;
        }
        return len;
    }
}

方法三:双指针

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //存在链表为空时,一定不相交
        if (headA == null || headB == null) return null;
        ListNode pA = headA;
        ListNode pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        //当指针 pA 和 pB 指向同一个节点或者都为 null 时,返回它们指向的节点或者null
        return pA;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一段使用GLib库的代码,主要实现了一个事件循环,并在事件循环中运行一些任务。具体的分析如下: 1. 第一行代码 `context.loop = g_main_loop_new(NULL, FALSE);` 创建了一个新的 GMainLoop 对象,并将其赋值给了 `context` 结构体中的 `loop` 成员变量。 2. 第二行代码 `g_main_loop_run(context.loop);` 开始了事件循环,程序在这里会一直等待,直到事件循环结束。 3. 第三、四、五行代码 `g_assert(!context.current_obj); g_assert(!context.current_messaging); g_assert(!context.current_sms);` 分别检查了 `context` 结构体中的三个成员变量是否为 NULL。如果这三个成员变量不为 NULL,程序会直接崩溃并输出错误信息。 4. 第六行代码 `g_main_loop_unref(context.loop);` 释放了 `context.loop` 成员变量所指向的 GMainLoop 对象。这个函数会减少 GMainLoop 对象的引用计数,如果引用计数为 0,那么这个对象就会被销毁。 5. 第七行代码 `g_clear_object(&context.connection); g_clear_object(&context.manager); g_clear_object(&context.properties);` 分别释放了 `context` 结构体中的三个 GObject 对象。这个函数会将对象指针设置为 NULL,并调用 g_object_unref() 函数释放对象。 6. 第八行代码 `g_list_free_full(g_steal_pointer(&context.objects), g_object_unref);` 释放了 `context.objects` 成员变量指向的链表。这个函数会遍历链表中的每个元素,并调用 g_object_unref() 函数释放对象。 7. 最后一行代码 `return 0;` 结束了整个程序的执行,返回 0 表示程序正常退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值