链表leetcode

 23. 合并K个升序链表(困难 重要 待做)

25. K 个一组翻转链表 (困难 重要)

class Solution(object):
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        p1 = head
        length = 0
        while p1:
            length+=1
            p1=p1.next
        pre = None
        cur = head
        for i in range(length//k):
            start = pre
            end = cur
            for j in range(k):
                temp = cur.next
                cur.next = pre
                pre = cur
                cur = temp
            if start:
                start.next = pre
            else:
                head = pre
            end.next = cur
            pre = end
        return head



2. 两数相加

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        # tmp是暂存(temporal)
        tmp = ListNode(0)  # 引用ListNode类定义了一个链表节点并赋给tmp
        # res是重置(reset)
        res = tmp  # 赋值
        # flag 标示
        flag = 0  # 初始化
        while l1 or l2:  # l1或l2不为空就持续执行
            tmp_sum = 0  # 链表节点值的和
            if l1: 
                tmp_sum = l1.val  # 把l1的某个节点的值赋给tmp_sum
                l1 = l1.next
            if l2:  
                tmp_sum += l2.val
                l2 = l2.next  # 指向下一个节点,为下一次的加和做准备
            tmp_res = ((tmp_sum + flag) % 10)  # 个位数字
            flag = ((tmp_sum + flag) // 10)  # 进位的数
            res.next = ListNode(tmp_res)
            res = res.next  # res后移
            if flag:  # 如果flag不为0,就是对应位置相加后有进位
                res.next = ListNode(1)  # res的下一节点设为1
        res = tmp.next  # 赋值
        del tmp  # 删除tmp变量
        return res  # 返回res链表

21. 合并两个有序链表

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        node = ListNode(None)
        new = node
        while l1 and l2:
            if l1.val > l2.val:
                new.next = l2
                l2 = l2.next
            else:
                new.next = l1
                l1 = l1.next
            new = new.next
        if l1:
            new.next = l1
        if l2:
            new.next = l2
        return node.next

160. 相交链表

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        pA = headA
        pB = headB
        while pA != pB:
            if pA:
                pA = pA.next
            else:
                pA = headB
            if pB:
                pB = pB.next
            else:
                pB = headA
        return pA

234. 回文链表

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head:
            return True
        slow = head
        fast = head
        # 快指针指向下两个,这样遍历之后,slow只走到中间节点
        while fast:
            slow = slow.next
            if fast.next:
                fast = fast.next.next
            else:
                fast = fast.next
         
        # 将中间节点之后的链表反转
        p, rev = slow, None
        while p:
            rev, rev.next, p = p, rev, p.next
        
        # 重新以head开始比较反转后的链表
        while rev:
            if rev.val != head.val:
                return False
            rev = rev.next
            head = head.next
        return True

206. 反转链表

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        pre = None
        cur = head
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre

141. 环形链表 

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        slow = head
        fast = head
        if head == None:
            return False
        while slow.next and fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

142. 环形链表 II

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow, fast = head, head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            # 如果相遇
            if slow == fast:
                p = head
                q = slow
                while p!=q:
                    p = p.next
                    q = q.next
                #你也可以return q
                return p

        return None

707. 设计链表 (待做)

138. 复制带随机指针的链表 (待做)

24. 两两交换链表中的节点

class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        h = ListNode(None)
        h.next = head
        pre = h
        while pre.next != None and pre.next.next != None:
            node1 = pre.next
            node2 = node1.next
            lat = node2.next

            pre.next = node2            
            node2.next = node1
            node1.next = lat
            
            pre = node1

        return h.next

上一篇:[LeetCode]21.合并两个有序链表(Java)


下一篇:L1-039 古风排版 (20 分)