def main(): datas = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] listnode = create_listnode(datas) print('raw datas: {}'.format(listnode)) s = Solution() print('k =2 {}'.format(s.reverseKGroup(listnode, 2))) listnode2 = create_listnode(datas) print('k = 3 {}'.format(s.reverseKGroup(listnode2, 3)))
def main(): datas = [1, 2, 3, 4, 5] listnode = create_listnode(datas) print('raw datas:{}'.format(listnode)) s = Solution() print('迭代:{}'.format(s.swapPairs0(listnode))) listnode2 = create_listnode(datas) print('递归:{}'.format(s.swapPairs1(listnode2)))
def main(): datas = [1, 2, 3, 4, 5] listnode = create_listnode(datas) print('{}'.format(listnode)) s = Solution() print('{}'.format(s.hasCycle1(listnode))) listnode2 = create_listnode(datas) print('{}'.format(s.hasCycle2(listnode2)))
def main(): datas = [1, 2, 3, 4, 5] listnode = create_listnode(datas) print('raw datas: {}'.format(listnode)) s = Solution() print('迭代:{}'.format(s.reverseList0(listnode))) listnode2 = create_listnode(datas) print('递归:{}'.format(s.reverseList1(listnode2)))
return dummy_head.next def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode: #递归 #时间复杂度O(n+m) 空间复杂度O(n+m) if not l1: return l2 elif not l2: return l1 elif l1.value <= l2.value: l1.next = self.mergeTwoLists2(l1.next, l2) return l1 elif l2.value < l1.value: l2.next = self.mergeTwoLists2(l1, l2.next) return l2 if __name__ == '__main__': s = Solution() print( '1', s.mergeTwoLists1(create_listnode([1, 2, 4]), create_listnode([1, 3, 4]))) print( '2', s.mergeTwoLists2(create_listnode([1, 2, 4]), create_listnode([1, 3, 4]))) print('1', s.mergeTwoLists1(create_listnode([]), create_listnode([]))) print('2', s.mergeTwoLists2(create_listnode([]), create_listnode([]))) print('1', s.mergeTwoLists1(create_listnode([]), create_listnode([0]))) print('2', s.mergeTwoLists2(create_listnode([]), create_listnode([0])))
class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: stack1 = [] stack2 = [] while l1: stack1.append(l1.value) l1 = l1.next while l2: stack2.append(l2.value) l2 = l2.next #时间复杂度O(max(m,n)) 空间复杂度O(n+m) ret = None add = 0 while stack1 or stack2 or add != 0: x = stack1.pop() if stack1 else 0 y = stack2.pop() if stack2 else 0 sum_ret = x + y + add add = sum_ret // 10 node = ListNode(sum_ret % 10) node.next, ret = ret, node return ret if __name__ == '__main__': s = Solution() l1 = create_listnode([7, 2, 4, 3]) l2 = create_listnode([5, 6, 4]) print('1', s.addTwoNumbers(l1, l2))
如果两个链表没有交点,返回 null. 在返回结果后,两个链表仍须保持原有的结构。 可假定整个链表结构中没有循环。 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。 ''' from src.common.list import create_listnode class Solution: def getIntersectionNode(self, headA, headB): if not headA or not headB: return None #时间复杂度O(n+m),空间复杂度O(1) a, b = headA, headB while a != b: a = a.next if a else headB b = b.next if b else headA return a if __name__ == '__main__': s = Solution() headA = create_listnode([4, 1, 8, 4, 5]) headB = create_listnode([5, 0, 1, 8, 4, 5]) print('1', s.getIntersectionNode(headA, headB)) headA = create_listnode([0, 9, 1, 2, 4]) headB = create_listnode([3, 2, 4]) print('1', s.getIntersectionNode(headA, headB)) headA = create_listnode([2, 6, 4]) headB = create_listnode([1, 5]) print('1', s.getIntersectionNode(headA, headB))
while r: if cur.value != r.value: return False r, cur = r.next, cur.next return True def isPalindrome2(self, head: ListNode) -> bool: if not head: return dummy_head = ListNode(-1) cur = head dummy_head.next = head pre = dummy_head while cur: cur.pre = pre cur, pre = cur.next, pre.next foot = pre while head: if head.val != foot.val: return False head, foot = head.next, foot.pre return True if __name__ == '__main__': s = Solution() head = create_listnode([1, 2, 1]) print(s.isPalindrome(head)) head = create_listnode([1, 2, 2, 1]) print(s.isPalindrome(head)) #
from src.common.list import create_listnode,ListNode class Solution: def partition(self, head: ListNode, x: int) -> ListNode: if not head:return s_dummy_head,l_dummy_head = ListNode(-1),ListNode(-1) small_foot, large_foot = s_dummy_head, l_dummy_head while head: if head.value < x: small_foot.next = head small_foot = small_foot.next else: large_foot.next = head large_foot = large_foot.next head = head.next large_foot.next = None small_foot.next = l_dummy_head.next return s_dummy_head.next if __name__ == '__main__': s = Solution() head = create_listnode([1, 4, 3, 2, 5, 2]) x = 3 print(head) print('1', s.partition(head, x)) head = create_listnode([2, 1]) x = 2 print(head) print('1', s.partition(head, x))
class Solution: def insertionSortList(self, head: ListNode) -> ListNode: #时间复杂度O(n*n) if not head: return None dummy_head = ListNode(-1) dummy_head.next = head last_sorted = head cur = head.next while cur: if last_sorted.value > cur.value: pre = dummy_head while pre.next.value <= cur.value: pre = pre.next pre.next, cur.next, last_sorted.next = cur, pre.next, cur.next else: last_sorted = last_sorted.next cur = last_sorted.next return dummy_head.next if __name__ == '__main__': s = Solution() head = create_listnode([4, 2, 1, 3]) # print(head) print('1', s.insertionSortList(head)) # head = create_listnode([-1,5,3,4,0]) # print(head) # print('1',s.insertionSortList(head))
''' 876. 链表的中间结点 给定一个头结点为 head 的非空单链表,返回链表的中间结点。 如果有两个中间结点,则返回第二个中间结点。 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4,5]) 返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。 注意,我们返回了一个 ListNode 类型的对象 ans,这样: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL. 示例 2: 输入:[1,2,3,4,5,6] 输出:此列表中的结点 4 (序列化形式:[4,5,6]) 由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。 提示: 给定链表的结点数介于 1 和 100 之间。 ''' from src.common.list import ListNode,create_listnode class Solution: def middleNode(self, head: ListNode) -> ListNode: mid = fast = head while fast and fast.next: mid, fast = mid.next, fast.next.next return mid if __name__ =='__main__': s = Solution() head = create_listnode([1, 2, 3, 4, 5, 6]) print('1',s.middleNode(head))
else: break succ = None if curr: succ = curr.next curr.next = None merged = merge(head1, head2) prev.next = merged while prev.next: prev = prev.next curr = succ subLength <<= 1 return dummyHead.next if __name__ == '__main__': s = Solution() head = create_listnode([4, 2, 1, 3]) print(head) print('1', s.sortList(head)) head = create_listnode([4, 2, 1, 3]) print('2', s.sortList2(head)) head = create_listnode([-1, 5, 3, 4, 0]) print(head) print('1', s.sortList(head)) head = create_listnode([-1, 5, 3, 4, 0]) print('2', s.sortList2(head))
输出: 2->3->6->7->1->5->4->NULL 说明: 应当保持奇数节点和偶数节点的相对顺序。 链表的第一个节点视为奇数节点,第二个节点视为偶数节点,以此类推。 ''' from src.common.list import ListNode, create_listnode class Solution: def oddEvenList(self, head: ListNode) -> ListNode: if not head: return head even_head = head.next odd, even = head, even_head while even and even.next: odd.next = even.next odd = odd.next even.next = odd.next even = even.next odd.next = even_head return head if __name__ == '__main__': s = Solution() head = create_listnode([1, 2, 3, 4, 5]) print('1', s.oddEvenList(head)) head = create_listnode([2, 1, 3, 5, 6, 4, 7]) print('1', s.oddEvenList(head))
new_head = new_head.next old_head = old_head.next return visited[head] def copyRandomList3(self, head): if not head: return #时间复杂度O(n) 空间复杂度O(1) cur = head while cur: node = ListNode(cur.value, True) cur.next, node.next, cur = node, cur.next, cur.next cur = head while cur: cur.next.random = cur.random.next if cur.random else None cur = cur.next.next old_head, new_head, ret = head, head.next, head.next while old_head: old_head.next = old_head.next.next if old_head.next else None new_head.next = new_head.next.next if new_head.next else None new_head = new_head.next old_head = old_head.next return ret if __name__ == '__main__': s = Solution() head = create_listnode([[7, None], [13, 0], [11, 4], [10, 2], [1, 0]]) print('1', s.copyRandomList(head)) print('2', s.copyRandomList2(head)) print('3', s.copyRandomList3(head))
返回同样按升序排列的结果链表。 示例 1: 输入:head = [1,1,2] 输出:[1,2] 示例 2: 输入:head = [1,1,2,3,3] 输出:[1,2,3] 提示: 链表中节点数目在范围 [0, 300] 内 -100 <= Node.val <= 100 题目数据保证链表已经按升序排列 ''' from src.common.list import create_listnode,ListNode class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if not head:return cur=head while cur and cur.next: if cur.value == cur.next.value: cur.next = cur.next.next else: cur = cur.next return head if __name__ =='__main__': s = Solution() head = create_listnode([1,1,2]) print('1',s.deleteDuplicates(head)) head = create_listnode([1, 1, 2, 3, 3]) print('1',s.deleteDuplicates(head))
class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy_head = ListNode(-1) dummy_head.next = head third_place = dummy_head first_place, second_place = head, head for _ in range(n): first_place = first_place.next while first_place: first_place, second_place, third_place = first_place.next, second_place.next, third_place.next third_place.next = second_place.next return dummy_head.next if __name__ == '__main__': s = Solution() head = [1, 2, 3, 4, 5] n = 2 print('1', s.removeNthFromEnd(create_listnode(head), n)) head = [1] n = 1 print('1', s.removeNthFromEnd(create_listnode(head), n)) head = [1, 2] n = 1 print('1', s.removeNthFromEnd(create_listnode(head), n)) head = [1, 2] n = 2 print('1', s.removeNthFromEnd(create_listnode(head), n))
tail.next, tail = e.node, e.node if e.node.next: heapq.heappush(q, E(e.node.next.value, e.node.next)) return dummy_head.next class E: def __init__(self, val, node): self.val = val self.node = node def __lt__(self, other): return self.val < other.val if __name__ == '__main__': s = Solution() lists = [] for l in [[1, 4, 5], [1, 3, 4], [2, 6]]: lists.append(create_listnode(l)) print(lists) print('1', s.mergeKLists(lists)) lists = [] for l in [[1, 4, 5], [1, 3, 4], [2, 6]]: lists.append(create_listnode(l)) print('2', s.mergeKLists2(lists)) lists = [] for l in [[1, 4, 5], [1, 3, 4], [2, 6]]: lists.append(create_listnode(l)) print('3', s.mergeKLists3(lists))
cur = s while cur and cur != tail: cur.next, cur, ret = ret, cur.next, cur return ret r = reverse(start, tail) pre.next = r return dummy_head.next def reverseBetween2(self, head: ListNode, left: int, right: int) -> ListNode: dummy_head = ListNode(-1) dummy_head.next = head pre = dummy_head for _ in range(left - 1): pre = pre.next cur = pre.next for _ in range(right - left): next = cur.next cur.next, next.next, pre.next = next.next, pre.next, next return dummy_head.next if __name__ == '__main__': s = Solution() head = create_listnode([1, 2, 3, 4, 5]) left, right = 2, 4 print('1', s.reverseBetween(head, left, right)) head = create_listnode([1, 2, 3, 4, 5]) print('2', s.reverseBetween2(head, left, right))