leetcode - 刷题记录-探索初级算法-链表

  1. 给定一个链表,删除链表的倒数第 个节点,并且返回链表的头结点。
    1. 示例:

      给定一个链表: 1->2->3->4->5, 和 n = 2.
      
      当删除了倒数第二个节点后,链表变为 1->2->3->5.
      

      说明:

      给定的 n 保证是有效的。

      进阶:

      你能尝试使用一趟扫描实现吗?

    2. # Definition for singly-linked list.
      # class ListNode:
      #     def __init__(self, x):
      #         self.val = x
      #         self.next = None
      
      class Solution:
          def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
              
              # solution1
              tmp = head
              length = 1
              while tmp.next != None:
                  length+=1
                  tmp = tmp.next
                  
              tmp = head
              # del_id + n == length+1
              if n==length:
                  # first
                  return head.next
              elif n==1:
                  # last
                  for i in range(length-2):
                      tmp = tmp.next
                  tmp.next = None
                  return head
              else:
                  del_id = length-n
                  for i in range(del_id-1):
                      tmp = tmp.next
                  tmp.next = tmp.next.next
                  return head
              
              # solution2
              tmp1 = head
              while 1:
                  tmp2 = tmp1
                  for i in range(n+1):
                      try:
                          tmp2 = tmp2.next
                      except:
                          return tmp1.next
                  if tmp2 is None:
                      tmp1.next = tmp1.next.next if n>1 else None
                      print(head)
                      return head
                  else:
                      tmp1 = tmp1.next

      两种方法均通过

  2. 反转链表

    1. 反转一个单链表。

      示例:

      输入: 1->2->3->4->5->NULL
      输出: 5->4->3->2->1->NULL

      进阶:
      你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

    2. # 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:
              
              # recursion
              if head is None:
                  return head
              res = head
              head = head.next
              res.next = None
              while head is not None:
                  tmp1 = head.next
                  head.next=res
                  res = head
                  head = tmp1
              return res
              
              # BP
              if head is None or head.next is None:
                  return head
              else:
                  tmp = head
                  sub = self.reverseList(head.next)
                  tmp.next.next = tmp
                  tmp.next = None
                  return sub

      通过。

  3. 合并两个有序链表

    1. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

    2. # Definition for singly-linked list.
      # class ListNode:
      #     def __init__(self, x):
      #         self.val = x
      #         self.next = None
      
      class Solution:
          def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
              if l1 is None or l2 is None:
                  head = l1 if l2 is None else l2
                  return head
              
              if l1.val > l2.val:
                  head = l2
                  l2 = l2.next
              else:
                  head = l1
                  l1 = l1.next
              res = head
                  
              while l1 is not None and l2 is not None:
                  if l1.val < l2.val:
                      res.next = l1
                      res = res.next
                      l1 = l1.next
                  else:
                      res.next = l2
                      res = res.next
                      l2 = l2.next        
              if l1 is None and l2 is not None:
                  res.next = l2
                  return head
              elif l2 is None and l1 is not None:
                  res.next = l1
                  return head

      通过

  4.  回文链表

    1. 请判断一个链表是否为回文链表。

      # Definition for singly-linked list.
      # class ListNode:
      #     def __init__(self, x):
      #         self.val = x
      #         self.next = None
      
      class Solution:
          def isPalindrome(self, head: ListNode) -> bool:
              dic = {}
              tmp = head
              p = 0
              while tmp is not None:
                  dic[p] = tmp.val
                  p  += 1
                  tmp = tmp.next
                  
              length = int(p/2)
              
              for i in range(length):
                  if dic[p-1-i] == head.val:
                      head = head.next
                  else:
                      return False
              return True

      通过

  5. 环形链表

    1. 给定一个链表,判断链表中是否有环。

    2. # 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:
              if head is None:
                  return False
              dic = {}
              while head is not None:
                  if head in dic:
                      return True
                  else:
                      dic[head] = head.val
                      head = head.next
              return False

      通过。选择合适的数据格式很有必要,字典的耗时远小于列表的。

发布了45 篇原创文章 · 获赞 1 · 访问量 8561

猜你喜欢

转载自blog.csdn.net/qq_32110859/article/details/104899976
今日推荐