【python数据结构】链表专题练习--选题来自Github大神CyC2018

按照CyC2018 (Github star 超过99k, 详情请见https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E9%93%BE%E8%A1%A8.md)总结的十道练习【python版】。

废话不多说,上菜~

  • 1. 找出两个链表的交点【leecode160】
    class Solution:
        def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
            ha, hb = headA, headB
            while ha != hb:
                ha = ha.next if ha else headB
                hb = hb.next if hb else headA
            return ha

     

  • 2. 链表反转【leetcode206】
    class Solution:
    
        # 头插法+迭代法
        def reverseList(self, head: ListNode) -> ListNode:
            if head is None:
                return
            pre = None
            cur = head
            while cur is not None:
                newhead = cur
                tempt = cur.next
                cur.next = pre
                pre = cur
                cur = tempt
            return newhead
    
    
        # 递归法
        def reverseList(self, head):
            if not head or not head.next:
                return head
            nextNode = self.reverseList(head.next)
            head.next.next = head
            head.next = None
            return nextNode

     

  • 3. 归并两个有序的链表【leetcode21】
    class Solution:
        def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
            if l1 is None and l2 is None:return
            elif l1 is None:return l2
            elif l2 is None:return l1
            else:
                if l1.val <= l2.val:
                    l1.next = self.mergeTwoLists(l1.next, l2)
                    return l1
                else:
                    l2.next = self.mergeTwoLists(l1, l2.next)
                    return l2

     

  • 4. 从有序链表中删除重复节点【leetcode83】
    class Solution:
        def deleteDuplicates(self, head: ListNode) -> ListNode:
            if not head:
                return
            pre = head
            cur = pre.next
            while cur:
                if pre.val == cur.val:
                    pre.next = cur.next
                    cur = cur.next
                else:
                    pre = pre.next
                    cur = cur.next
            return head

     

  • 5. 删除链表的倒数第 n 个节点【leetcode19】
    class Solution:
        def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
            if not head: return 
            mystack = []
            node = head
            while node:
                mystack.append(node)
                node = node.next
            if n == len(mystack):
                head = head.next
                return head
            else:
                mystack[-(n+1)].next = mystack[-n].next
                return head

     

  • 6. 交换链表中的相邻结点【leetcode24】
    class Solution:
        def swapPairs(self, head: ListNode) -> ListNode:
            if not head:
                return 
            thead = ListNode(-1)
            thead.next = head
            c = thead
            while c.next and c.next.next:
                a = c.next
                b = c.next.next
                c.next = b
                temp = b.next
                b.next = a
                a.next = temp
                c = c.next.next
            return thead.next

     

  • 7. 链表求和【leetcode445】
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            mystack1 = []
            mystack2 = []
            while l1:
                mystack1.append(l1.val)
                l1 = l1.next
            while l2:
                mystack2.append(l2.val)
                l2 = l2.next
            mystack1 = [str(i) for i in mystack1]
            mystack2 = [str(i) for i in mystack2]
            num = int(''.join(mystack1)) + int(''.join(mystack2))
            num = list(str(num))
            thead = ListNode(-1)
            node = ListNode(int(num[0]))
            thead.next = node
            for n in num[1:]:
                node.next = ListNode(int(n))
                node = node.next
            return thead.next

     

  • 8. 回文链表
  • class Solution:
        # 时间复杂度O(n),空间复杂度O(n)
        def isPalindrome(self, head: ListNode) -> bool:
            if not head:
                return True
            stack = []
            cur = head
            while cur:
                stack.append(cur.val)
                cur = cur.next
            while stack:
                if len(stack)>1:
                    left = stack.pop(0)
                    right = stack.pop()
                    if left != right:
                        return False
                else:
                    return True
            return True
    
           
    class Solution:
        # 时间复杂度O(n),空间复杂度O(1)
        def isPalindrome(self, head: ListNode) -> bool:
            slow = head
            fast = head
            prev = None
            flag = True
            # step1: reverse
            while(fast and fast.next):
                fast = fast.next.next
                temp = slow.next
                slow.next = prev
                prev = slow
                slow = temp
            # step2: compare
            if fast: # 奇数
                node1 = prev
                node2 = slow.next
            else: # 偶数
                node1 = prev
                node2 = slow
            while(node1 and node2):
                if node1.val != node2.val:
                    flag = False
                node1, node2 = node1.next, node2.next
            # step3: recover(optional)
            back = slow
            while(prev):
                temp = prev.next
                prev.next = back
                back = prev
                prev = temp
            return flag

     

  • 9. 分隔链表
  • class Solution:
        def split(self, length, k):
            base = length // k
            remain = length % k
            print(remain)
            res = [base]*k
            while remain > 0:
                res[remain-1] += 1
                remain -= 1
            return res
    
        def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
            if not root:
                return [None]*k
            length = 0
            node = root
            while node:
                length += 1
                node = node.next
            nums = self.split(length, k)
            i = 0
            res = []
            temp = root
            while i < k:
                n = nums[i]
                j = 0
                root_i = ListNode(-1)
                temp_j = root_i
                while j < n:
                    temp_j.next = temp
                    temp = temp.next
                    temp_j = temp_j.next
                    j += 1
                temp_j.next = None
                res.append(root_i.next)
                i += 1
            return res

     

  • 10. 链表元素按奇偶聚集
class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:return head
        odd = head
        even_head = even = head.next
        while odd.next and even.next:
            odd.next = odd.next.next
            even.next = even.next.next
            odd,even = odd.next,even.next
        odd.next = even_head
        return head

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值