• Leetcode-反转链表


    206.逆转链表
    Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
    方法一:迭代
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseList(self, head: ListNode) -> ListNode:
            pre, cur = None, head
            while cur:
                    pre, cur, cur.next = cur, cur.next, pre
            return pre 
    iterative

    方法二:递归
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseList(self, head: ListNode) -> ListNode:
            if not head or not head.next:
                return head
            p = self.reverseList(head.next)
            head.next.next = head
            head.next=None
            return p
    recursive
    24.两两交换链表
    Given 1->2->3->4, you should return the list as 2->1->4->3.
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def swapPairs(self, head: ListNode) -> ListNode:
            pre,pre.next = self,head
            while pre.next and pre.next.next:
                a = pre.next
                b = a.next
                a,b.next,b = b,a,b.next
                pre = a 
            return self.next
    iterative

     
     
     
    141.回环链表
    Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node.

    方法一:设置时间暴力循环
    方法二:判断是否添加过该元素
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def hasCycle(self, head: ListNode) -> bool:
            l = []
            while head != None:
                if head.val in l:
                    return True
                l.append(head)
                head=head.next
            return False       
    添加进列表
    方法三:双步运行
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def hasCycle(self, head: ListNode) -> bool:
            fir = sec =head
            while fir and sec and sec.next:
               fir = fir.next
               sec = sec.next.next
               if fir == sec:
                    return True
            return False     
    双步
     
    25. Reverse Nodes in k-Group

    Given this linked list: 1->2->3->4->5

    For k = 2, you should return: 2->1->4->3->5

    For k = 3, you should return: 3->2->1->4->5

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
            if  head:
                node = head
                for _ in range(k-1):
                    node = node.next
                    if not node:
                        return head
                prev = self.reverseKGroup(node.next,k)
                
                while prev != node:
                    prev, head.next, head = head, prev, head.next
                return prev
    反转K为一组节点
     
  • 相关阅读:
    PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
    PAT Advanced 1029 Median (25) [two pointers]
    PAT Advanced 1010 Radix(25) [⼆分法]
    PAT Basic 1070 结绳(25) [排序,贪⼼]
    PAT Basic 1023 组个最⼩数 (20) [贪⼼算法]
    PAT Basic 1020 ⽉饼 (25) [贪⼼算法]
    PAT Advanced 1070 Mooncake (25) [贪⼼算法]
    PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
    PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
    PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]
  • 原文地址:https://www.cnblogs.com/ybxw/p/12724747.html
Copyright © 2020-2023  润新知