美国求职-刷题Day4

LeetCode21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4

做这道题,最开始没有想到递归,一头雾水,以后应该有个条件反射,看到链表就该有递归的意识。
再来看递归的三要素

  • 终止条件:两条链表分别名为 l1 和 l2,当 l1 为空或 l2 为空时结束
  • 返回值:每一层调用都返回排序好的链表头
  • 本级递归内容:如果 l1 的 val 值更小,则将 l1.next 与排序好的链表头相接,l2 同理

复杂度为O(m+n),mm 为 l1的长度,nn 为 l2 的长度

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

        if (l2.val > l1.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }

再来几道linked list的题目

83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2 Output: 1->2
Example 2:

Input: 1->1->2->3->3 Output: 1->2->3

这道题有了刚刚的势头,只要扣住1、递归中断条件 2、每次的递归结果 3、最终返回结果就好说了:1、当链表穷尽了就结束 2、如果有重复元素,就跳过该节点,接着判断;否则就开始遍历下一个节点;3、最后返回筛选过的列表

 public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.next.val == head.val) {
            head.next = head.next.next;
            head = deleteDuplicates(head);
        } else {
            head.next = deleteDuplicates(head.next);
        }
      
        return head;
    }

82. Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5 Output: 1->2->5
Example 2:

Input: 1->1->1->2->3 Output: 2->3

这个题在刚刚的题目上做了升级,其实如果是双向链表的会很简单,判断到相同的直接a->prior->next = a->next就完事了,单链表的话需要我们人为创造一个a->prior

   if (head == null || head.next == null) {
            return head;
        }

        ListNode preHead = new ListNode(-1);
        ListNode prior = preHead;
        preHead.next = head;
        ListNode current = head;

        while (current.next != null) {
            if (current.next.val != current.val) {
                prior = current;
                current = current.next;
            } else {
                while (current.next != null && current.val == current.next.val) {
                    current = current.next;
                }
                if (current.next != null) {
                    prior.next = current.next;
                    current = current.next;
                } else {
                    prior.next = null;
                }
            }
        }
        return preHead.next;

再回过头来试试递归的方式

    public ListNode deleteDuplicates3(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        if (head.next.val == head.val) {
            while (head.next != null && head.next.val == head.val) {
                head = head.next;
            }
            return deleteDuplicates(head.next);
        } else {
            head.next = deleteDuplicates(head.next);
        }

        return head;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值