def addLists(n1, n2): if (n1 is None) or (n2 is None): return None head = ListNode(0) cur = head prev = None carry = 0 while (n1 is not None) or (n2 is not None): val = carry if n1 is not None: val += n1.val n1 = n1.nxt if n2 is not None: val += n2.val n2 = n2.nxt cur.val = val % 10 carry = val // 10 cur.nxt = ListNode(0) prev = cur cur = cur.nxt if carry: cur.val = carry else: prev.nxt = None return head
def __init__(self, repo, parent=None): all_heads = [GitTreeNode(head.object.tree, parent=self, name=head.name) for head in repo.heads] children = [ GitTreeNode(repo.head.object.tree, parent=self, name=repo.head.name), ListNode(self, all_heads, name="all heads"), ] ListNode.__init__(self, parent, children) self.repo_ = repo
def test(): n1 = ListNode(3) appendToTail(n1, 1) appendToTail(n1, 7) n2 = ListNode(5) appendToTail(n2, 9) appendToTail(n2, 2) printList(n1) printList(n2) s = addLists(n1, n2) printList(s)
def addAtTail(self, val: int) -> None: """ Append a node of value val to the last element of the linked list. """ if not self.tail: self.tail = self.head = ListNode(val) return self.tail.next = ListNode(val) self.tail = self.tail.next self.size += 1
def add_two_numbers(self, l1: ListNode, l2: ListNode) -> ListNode: flag = 0 add_sum, flag = self.add_two(l1.val, l2.val, flag) _start = start = ListNode(add_sum) while l1.next is not None or l2.next is not None or flag == 1: l1_next_val, l1 = (0, l1) if l1.next is None else (l1.next.val, l1.next) l2_next_val, l2 = (0, l2) if l2.next is None else (l2.next.val, l2.next) add_sum, flag = self.add_two(l1_next_val, l2_next_val, flag) start.next = ListNode(add_sum) start = start.next return _start
def addTwoNums(l1, l2): l1Num = l2Num = '' n1, n2 = l1, l2 while True: if n1 is not None: l1Num += str(n1.val) if n1.next is None: break else: n1 = n1.next while True: if n2 is not None: l2Num += str(n2.val) if n2.next is None: break else: n2 = n2.next sumNum = int(l1Num[::-1]) + int(l2Num[::-1]) r1 = str(sumNum)[::-1] r2 = list(map(lambda x: ListNode(x), r1)) for i in range(len(r1) - 1): r2[i].next = r2[i + 1] node = r2[0] return node
def reverse_list(L): head = ListNode('HEAD', L) list_iter = head.next while list_iter.next: temp = list_iter.next list_iter.next, temp.next, head.next = temp.next, head.next, temp return head.next
def addAtIndex(self, index: int, val: int) -> None: """ Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. """ prev, p, count = None, self.head, 0 while p: if count == index: # insert at position here break prev = p p = p.next count += 1 if count == 0: self.addAtHead(val) return if count == self.size: self.addAtTail(val) return if count > self.size: return assert prev is not None prev.next = ListNode(val) prev.next.next = p self.size += 1
def reverseList2(self, head: ListNode) -> ListNode: if not head or head.next == None: return head res = self.reverseList2(head.next) head.next.next = head head.next = None return res
def removeNthFromEnd3(self, head: ListNode, n: int) -> ListNode: if not head: self.count = 0 return head head.next = self.removeNthFromEnd3(head.next, n) self.count += 1 return head.next if self.count == n else head
def removeNthFromEnd2(self, head: ListNode, n: int) -> ListNode: # dummy,添加头节点 dummy = ListNode(0) dummy.next = head # 快指针先走 n 步 slow, fast = dummy, dummy for _ in range(n): fast = fast.next # 快慢指针同时走,直到 fast 指针达到链表尾部节点,此时 slow 到达倒数第 n 个节点的前一个节点 while fast and fast.next: slow, fast = slow.next, fast.next # 删除节点并重新链接 slow.next = slow.next.next return dummy.next
def list_value(self): self.tree.append(ListNode()) while self.token[0] in (token_types.atom, token_types.string): if self.token[0] == token_types.atom: self.atom() else: self.value() self.expect(token_types.list_end) self.tree.pop()
def merge(self, l1: ListNode, l2: ListNode) -> ListNode: """Merge two lists""" root = pre = ListNode() while l1 and l2: if l1.val < l2.val: pre.next = ListNode(l1.val) l1 = l1.next else: pre.next = ListNode(l2.val) l2 = l2.next pre = pre.next if l1: pre.next = l1 if l2: pre.next = l2 return root.next
def test(): n = ListNode(3) appendToTail(n, 1) appendToTail(n, 4) appendToTail(n, 2) appendToTail(n, 3) appendToTail(n, 3) appendToTail(n, 2) printList(n) deleteDupsWithoutBuffer(n) printList(n)
def test(): n = ListNode(3) appendToTail(n, 1) appendToTail(n, 4) appendToTail(n, 2) appendToTail(n, 3) appendToTail(n, 3) appendToTail(n, 2) printList(n) deleteNode(n) printList(n)
def hasCycle5(self, head: ListNode) -> bool: # 5. 标记val while head: # s = getattr(head, 's', None) # 获取s属性 # if s: # 判断s属性 if head.val == '1': return True # head.s = 1 # 添加s属性 head.val = '1' head = head.next return False
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: # dummy,添加头节点 dummy = ListNode(0) dummy.next = head # 获取链表长度 cur, lenth = head, 0 while cur: lenth += 1 cur = cur.next # 找到倒数第 n 个节点前的那个节点 cur = dummy # 以示例为例,注意长度是 5 而不是 4 for _ in range(lenth - n): cur = cur.next # 删除节点并重新链接 cur.next = cur.next.next return dummy.next
def test(): n = ListNode(3) appendToTail(n, 1) appendToTail(n, 4) appendToTail(n, 2) appendToTail(n, 3) appendToTail(n, 3) appendToTail(n, 2) printList(n) print("4th to last:", nthToLast(n, 4)) print("6th to last:", nthToLast(n, 6))
def addAtHead(self, val: int) -> None: """ Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. """ tmp = self.head self.head = ListNode(val) self.head.next = tmp if not self.tail: self.tail = self.head self.size += 1
def reverse_sublist(L, s, f): head = sublist_head = ListNode('HEAD', L) for _ in range(1, s): sublist_head = sublist_head.next sublist_iter = sublist_head.next for _ in range(f - s): temp = sublist_iter.next sublist_iter.next, temp.next, sublist_head.next = temp.next, sublist_head.next, temp return head.next
def add(self, item): ''' adds an item to the list ''' if item is not instanceof(Node): item = ListNode(item) if self.head is None: self.head = item else: self.tail.next = item self.tail = item return
def merge_lists(L1, L2): res = head = ListNode(data='HEAD') tail = ListNode() L1, L2 = L1.next, L2.next # filter out dummy head nodes while L1 and L2: if L1.data <= L2.data: head.next = L1 L1 = L1.next else: # L2.data < L1.data head.next = L2 L2 = L2.next head = head.next while L1: head.next = L1 L1 = L1.next head = head.next while L2: head.next = L2 L2 = L2.next head = head.next head = tail return res
def merge_sorted_lists(L1, L2): """ Assume L1 and L2 are sorted """ head = tail = ListNode('HEAD') while L1 and L2: if L1.data <= L2.data: tail.next = L1 L1 = L1.next else: # L2.data < L1.data tail.next = L2 L2 = L2.next tail = tail.next # print(head) # print("L1", L1) # print("L2", L2) tail.next = L1 or L2 return head.next
def test(): a = ListNode('A') b = ListNode('B') c = ListNode('C') d = ListNode('D') e = ListNode('E') a.nxt = b b.nxt = c c.nxt = d d.nxt = e e.nxt = c print(findBeginning(a).val)
return head.next def reverse_list(L): head = ListNode('HEAD', L) list_iter = head.next while list_iter.next: temp = list_iter.next list_iter.next, temp.next, head.next = temp.next, head.next, temp return head.next if __name__ == '__main__': print('Revers a single sublist!') n2 = ListNode(2) n7 = ListNode(7, n2) n5 = ListNode(5, n7) n3 = ListNode(3, n5) n11 = ListNode(11, n3) L = n11 print("L -> {}".format(L)) L = reverse_sublist(L, 2, 4) print("L -> {}".format(L)) n2 = ListNode(2) n7 = ListNode(7, n2) n5 = ListNode(5, n7) n3 = ListNode(3, n5) n11 = ListNode(11, n3)
sumNum = int(l1Num[::-1]) + int(l2Num[::-1]) r1 = str(sumNum)[::-1] r2 = list(map(lambda x: ListNode(x), r1)) for i in range(len(r1) - 1): r2[i].next = r2[i + 1] node = r2[0] return node # Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) # Output: 7 -> 0 -> 8 n2 = ListNode(2) n4 = ListNode(4) n3 = ListNode(3) n2.next = n4 n4.next = n3 l5 = ListNode(5) l6 = ListNode(6) l4 = ListNode(4) l5.next = l6 l6.next = l4 r = addTwoNums(n2, l5) while r is not None: print(r.val)
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: """""" nodeA, nodeB = headA, headB while nodeA != nodeB: nodeA = nodeA.next if nodeA else headB nodeB = nodeB.next if nodeB else headA return nodeA if __name__ == '__main__': s = Solution() # [4, 1, 8, 4, 5] # [5, 6, 1, 8, 4, 5] nodei = ListNode(8) nodei.next = ListNode(4) nodei.next.next = ListNode(5) headA = ListNode(4) headA.next = ListNode(1) headA.next.next = nodei headB = ListNode(5) headB.next = ListNode(6) headB.next.next = ListNode(1) headB.next.next.next = nodei # [2,6,4] # [1, 5] # headA = ListNode(2)
head.next = L1 L1 = L1.next head = head.next while L2: head.next = L2 L2 = L2.next head = head.next head = tail return res if __name__ == '__main__': print("Merging two sorted linked lists!") n7 = ListNode(data=7) n5 = ListNode(data=5, next_node=n7) n2 = ListNode(data=2, next_node=n5) L1 = ListNode(data='HEAD', next_node=n2) # always start with a dummy node n11 = ListNode(data=11) n3 = ListNode(data=3, next_node=n11) L2 = ListNode(data='HEAD', next_node=n3) print("L1 -> {}".format(L1)) print("L2 -> {}".format(L2)) print("merged -> {}".format(merge_lists(L1, L2))) print("-----") n7 = ListNode(data=7) n5 = ListNode(data=5, next_node=n7)