• Leetcode之链表(前200道)


    持续更新...

    github链接:https://github.com/x2mercy/Leetcode_Solution

    Easy

    1. 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.

    这道题有两种解法,一种是iterator,一种是recursively

    iterator:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            result=current=ListNode(0)
            if not l1:
                return l2
            if not l2:
                return l1
            while l1 and l2:
                if l1.val<l2.val:
                    current.next=l1
                    l1=l1.next
                else:
                    current.next=l2
                    l2=l2.next
                current=current.next
            current.next=l1 or l2
            return result.next

    recursively:

    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            if not l1: 
                return l2
            if not l2:
                return l1
            if l1.val==l2.val:
                l1.next=self.mergeTwoLists(l1.next,l2)
                return l1
            if l1.val<l2.val:
                l1.next=self.mergeTwoLists(l1.next,l2)
                return l1
            if l1.val>l2.val:
                l2.next=self.mergeTwoLists(l2.next,l1)
                return l2

    这道题用了两种方法:recursively和iterator。 recursively比较难理解,画出stack模型会比较容易理解,主要是通过反复调用方法来实现。 iterator比较容易理解,主要思想是创造两个新的链表,result和current,current负责存储通过比较l1和l2得出的结果的节点, result负责最后的返回,result=current=ListNode(0)。要注意:每次比较完之后,要用l1=l1.next来移动到下一个节点进行比较 同时,每一次循环最后都需要current=current.next来移动到下一个节点。当l1或者l2的节点为none时,跳出循环,使用current.next=l1orl2 来将另外一个节点剩余节点存入current,最后return result.next

    2. Remove Duplicates from Sorted List:

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

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            current=head
            if not current:
                return None
            while current.next:
                if current.next.val==current.val:
                    current.next=current.next.next
                else:
                    current=current.next
            return head
    1.single-linked list的head不是空
    2.最后返回是return head,可以返回整个list

    3. Linked List Cycle:
    Given a linked list, determine if it has a cycle in it.
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution(object):
        def hasCycle(self, head):
            """
            :type head: ListNode
            :rtype: bool
            """
            try:
                slow=head
                fast=head.next
                while slow is not fast:
                    slow=slow.next
                    fast=fast.next.next
                return True
            except:
                return False
                
                
            
    这道题要mark一下
    很有意思的算法,设了两个指针,一个快指针,一个慢指针,快指针每次.next.next,慢指针.next,用了try...except,如果是有cycle的话,
    慢指针和快指针总会遇到,于是返回true
    如果没有(except),返回false
    4.Intersection of Two Linked Lists:
    Write a program to find the node at which the intersection of two singly linked lists begins.
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            a=headA
            b=headB
            if a is None or b is None:
                return None
    
            while a is not b:
                if a is None:
                    a=headB
                else:
                    a=a.next
                if b is None:
                    b=headA
                else:
                    b=b.next
            return a
        
    
    
    这个算法也要mark一下!很机智!
    核心就是指向两个链表节点的指针一起移动,但是最tricky的地方在于,由于两个链表长度不同,到达intersection的时间也不同,所以可能跑完整个链表
    都不会相遇,所以每个链表跑完之后(=none),则指向另一个链表的head,继续跑,这样每次都在缩短两个链表到达intersection的距离,于是最后在
    intersection完美相遇!
     
  • 相关阅读:
    Zend Guard 7 , Zend Guard Loader处理PHP加密
    [转]pHP源码加密方法调查
    使用USB Key(加密狗)实现身份认证
    4*4行列式矩阵键盘
    HDU-2546-饭卡
    HDU-2073-无限的路
    HDU-2065-"红色病毒"问题
    HDU-2063-过山车
    HDU-2060-Snooker
    HDU-2056-Rectangles
  • 原文地址:https://www.cnblogs.com/x1mercy/p/7807613.html
Copyright © 2020-2023  润新知