Leetcode 题解 - 链表

文章放置于:https://github.com/zgkaii/CS-Notes-Kz,欢迎批评指正!

1. 找出两个链表的交点

160. Intersection of Two Linked Lists (Easy)

Leetcode / 力扣

设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。

当访问 A 链表的指针访问到链表尾部时,令它从链表 B 的头部开始访问链表 B;同样地,当访问 B 链表的指针访问到链表尾部时,令它从链表 A 的头部开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。

两者速度一致,相同时间走的路程一致,那么会同一时间到达终点。

如果不存在交点,那么 a + b = b + a,以下实现代码中 l1 和 l2 会同时为 null,从而退出循环。

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
   
        if (headA == null || headB == null) return null;

        ListNode p1 = headA, p2 = headB;
        while (p1 != p2) {
   
            p1 = (p1 == null) ? headB : p1.next;
            p2 = (p2 == null) ? headA : p2.next;
        }
        return p1;
    }

2. 链表反转

206. Reverse Linked List (Easy)

Leetcode / 力扣

递归:

public ListNode reverseList(ListNode head) {
   
    if (head == null || head.next == null) return head;
    
    ListNode next = head.next;
    ListNode newHead = reverseList(next);// 从当前节点的下一个结点开始递归调用。
    next.next = head;// head挂到next节点的后面就完成了链表的反转。
    head.next = null;// 这里head相当于变成了尾结点,尾结点都是为空的,否则会构成环。
    
    return newHead;
}

头插法:

public ListNode reverseList(ListNode head) {
   
    ListNode newHead = new ListNode(-1);
    while (head != null) {
   
        ListNode next = head.next;
        head.next = newHead.next;
        newHead.next = head;
        head = next;
    }
    return newHead.next;
}

双指针:

    public ListNode reverseList(ListNode head) {
   
        ListNode cur = head, end = null;
        
        while (cur != null) {
   
            ListNode tmp = cur.next;// 每次访问的原链表节点都会成为新链表的头结点
            cur.next = end;
            end = cur;// 更新新链表
            cur = tmp;
        }
        return end;
    }

3. 归并两个有序的链表

21. Merge Two Sorted Lists (Easy)

Leetcode / 力扣

递归:

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   
		if(l1 == null) return l2;
		if(l2 == null) return l1;
		if(l1.val < l2.val){
   
			l1.next = mergeTwoLists(l1.next, l2);
			return l1;
		} else{
   
			l2.next = mergeTwoLists(l1, l2.next);
			return l2;
		}
    }

迭代:

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   
        if (l1 == null) return l2;
        if (l2 == null) return l1;

        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while(l1 != null && l2!= null){
   
            if(l1.val <= l2.val){
   
                cur.next = l1;
                l1 = l1.next;
            }else {
   
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = (l1 == null) ? l2 : l1;
        return dummy.next;
    }

4. 环形链表

141. Linked List Cycle(Easy)

Leetcode / 力扣

    public boolean hasCycle(ListNode head) {
   
        if (head == null || head.next == null) return false;
        ListNode slow = head, fast = head.next;
        
        while (slow != fast) {
   
            if (fast == null || fast.next == null) return false;
            slow = slow.next;
            fast = fast.next.next;
        }
        return true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值