题解:链表相关问题及题解(Python)

一、链表反转
1.1 头插法

class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        pre = ListNode(-1)
        pre.next = None
        while head:
            temp = head.next
            head.next = pre.next
            pre.next = head
            head = temp
        return pre.next

1.2 双指针法

class Solution:
    def ReverseList(self , head: ListNode) -> ListNode:
        pre = None
        cur = head
        while cur:
            temp = cur.next
            cur.next = pre
            # 双指针向后移动
            pre = cur
            cur = temp
        return pre

二、链表指定区间反转
2.1 头插法

class Solution:
    def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        # write code here
        pre = ListNode(-1)
        pre.next = head
        for i in range(m-1):
            pre = pre.next
        cur = pre.next
        pre.next = None
        last = cur
        for i in range(m,n+1):
            temp = cur.next
            cur.next = pre.next
            pre.next = cur
            cur = temp
        last.next = cur
        return head

2.2 双指针法

class Solution:
    def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        # write code here
        pre = ListNode(-1)
        pre.next = head
        for i in range(m-1):
            pre = pre.next
        cur = pre.next
        for i in range(m,n):
            temp = cur.next
            cur.next = temp.next
            temp.next = pre.next
            pre.next = temp
        return head

三、链表每k个为一组反转
3.1 递归

class Solution:
    def reverseKGroup(self , head: ListNode, k: int) -> ListNode:
        # write code here
        tail = head
        for i in range(k):
            if tail == None:
                return head
            else:
                tail = tail.next
        pre = None
        cur = head
        while cur != tail:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        head.next = self.reverseKGroup(tail,k)
        return pre

四、合并有序链表
4.1递归

class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        if pHead1 == None or pHead2 == None:
            return  pHead1 if pHead2 == None else pHead2
        if pHead1.val > pHead2.val:
            pHead2.next = self.Merge(pHead1, pHead2.next)
            return pHead2
        else:
            pHead1.next = self.Merge(pHead1.next, pHead2)
            return pHead1

4.2 双指针

class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        res = ListNode(-1)
        cur = res
        while pHead1 and pHead2:
            if pHead1.val >= pHead2.val:
                cur.next = pHead2
                pHead2 = pHead2.next
            else:
                cur.next = pHead1
                pHead1 = pHead1.next
            cur = cur.next
        if pHead1:
            cur.next = pHead1
        else:
            cur.next = pHead2
        return res.next

五、判断链表是否有环
5.1 追及问题(双指针)

class Solution:
    def hasCycle(self , head: ListNode) -> bool:
        p1 = head
        p2 = head
        while p2 and p2.next :
            p1 = p1.next
            p2 = p2.next.next
            if p1 == p2:
                return True
        return False

5.2 哈希表

class Solution:
    def hasCycle(self , head: ListNode) -> bool:
        visited = set({})
        p = head
        while p:
            if p not in visited:
                visited.add(p)
                p = p.next
            else:
                return True
        return False

六、寻找入环节点

class Solution:
    def hasCycle(self , head: ListNode):
        p1 = head
        p2 = head
        while p2 and p2.next :
            p1 = p1.next
            p2 = p2.next.next
            if p1 == p2:
                return True, p1
        return False, p1
    def EntryNodeOfLoop(self, pHead):
        # write code here 
        loop, p1 = self.hasCycle(pHead)
        p2 = pHead
        if loop ==1:
            while p1 != p2:
                p1 = p1.next
                p2 = p2.next
            return p1
        else:
            return None

七、链表排序
7.1 借助列表

class Solution:
    def sortInList(self , head: ListNode) -> ListNode:
        # write code here
        p = head
        list_ = []
        while p:
            list_.append(p.val)
            p = p.next
        list_ = sorted(list_,reverse=True)
        
        res = ListNode(float('-inf'))
        res.next = None
        for a in list_:
            node = ListNode(a)
            node.next = res.next
            res.next = node
        return res.next

7.2 双指针+递归

class Solution:
    def Merge(self, pHead1: ListNode, pHead2: ListNode):
        p1 = pHead1
        p2 = pHead2
        res = ListNode(-1)
        cur = res
        while p1 and p2:
            if p1.val > p2.val:
                cur.next = p2
                p2 = p2.next
            else:
                cur.next = p1
                p1 = p1.next
            cur = cur.next
        if p1:
            cur.next = p1
        if p2:
            cur.next = p2
        return res.next
            
    def sortInList(self , head: ListNode) -> ListNode:
        # write code here
        if head==None or head.next==None:
            return head
        fast = head.next
        slow = head
        while fast:
            if fast.next == None:
                break
            slow = slow.next
            fast = fast.next.next
        head2 = slow.next
        slow.next = None
        head1 = self.sortInList(head)
        head2 = self.sortInList(head2)
        head = self.Merge(head1,head2)
        return head

八、判断是否是回文链表
8.1 利用数组
8.2 快慢指针 + 链表逆序

class Solution:
    def isPail(self , head: ListNode) -> bool:
        # write code here
        mid = self.Middle(head)
        r_head = self.Reverse(mid)
        while r_head and head:
            if r_head.val != head.val:
                return False
            r_head = r_head.next
            head = head.next
        return True
    def Middle(self, head):
        slow = head
        fast = head
        while fast and fast.next and fast.next.next:
            fast = fast.next.next
            slow = slow.next
        return slow
    def Reverse(self, head):
        pre = ListNode(-1)
        pre.next = None
        while head:
            temp = head.next
            head.next = pre.next
            pre.next = head
            head = temp
        return pre.next

你可能感兴趣的:(算法&题解,链表,数据结构,算法)