技術分享

LeetCode

Leetcode

160. Intersection of Two Linked Lists

160. Intersection of Two Linked Lists

Leetcode 分享筆記160. Intersection of Two Linked Lists題目 # 答題線索: 被找到的點後面的Linked List的值會相同,所以可以用Set存已經存在的Linked List,從中找出是否有接續一樣的值,如果有代表這個ListNode即是交點 nodeSet = set() while headA != None: if headA not in nodeSet: nodeSet.add(headA) headA = headA.next while headB: if headB...

817. Linked List Components

817. Linked List Components

Leetcode 分享筆記817. Linked List Components題目 count = 0 hasConnection = False while head: if head.val in nums: hasConnection = True elif hasConnection: count += 1 hasConnection = False head = head.next if hasConnection: count += 1 return...

234. Palindrome Linked List

234. Palindrome Linked List

Leetcode 分享筆記234. Palindrome Linked List題目 class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ # 從頭開始與從尾巴開始,如果一樣代表回文。用兩個List來做比對 curr = head pre = None head_list = [] while curr != None: head_list.append(curr.val) next =...

141. Linked List Cycle

141. Linked List Cycle

Leetcode 分享筆記141. Linked List Cycle題目 class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ while head and head.next: if str(head.val) == "visited": return True head.val = "visited" head = head.next return...

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

Leetcode 分享筆記83. Remove Duplicates from Sorted List題目 class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return head curr = head while curr.next != None: if curr.val == curr.next.val: curr.next =...

1290. Convert Binary Number in a Linked List to Integer

1290. Convert Binary Number in a Linked List to Integer

Leetcode 分享筆記1290. Convert Binary Number in a Linked List to Integer題目 class Solution(object): def getDecimalValue(self, head): """ :type head: ListNode :rtype: int """ curr = head ans = [] output = 0 while curr != None: ans.append(curr.val) curr = curr.next for i in...

divider2 3 scaled

Get Instant Access to Financial News & Advice

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation

77 / 100