def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ carryNum = 0 work1 = l1 work2 = l2 preResult = ListNode(0) temp = preResult while work1 != None or work2 != None: if work1 != None : carryNum += work1.val work1 = work1.next if work2 != None : carryNum += work2.val work2 = work2.next temp.next = ListNode(carryNum % 10) temp = temp.next if carryNum > 9 : carryNum = 1 else : carryNum = 0 if carryNum > 0 : temp.next = ListNode(1) return preResult.next
def list2linked(nums: List[int]) -> ListNode: head = ListNode(nums[0]) curr = head for i in range(1, len(nums)): curr.next = ListNode(nums[i]) curr = curr.next return head
def add(self, value): tmp_node = ListNode(value) if self._head_node is None: self._head_node = tmp_node else: tmp_node.set_next_node(self._head_node) self._head_node = tmp_node
def reverseList(self, head: ListNode) -> ListNode: tmpNode = head newNode = None while tmpNode != None: newFlagListNode = ListNode(tmpNode.val) newFlagListNode.next = newNode newNode = newFlagListNode tmpNode = tmpNode.next return newNode
def num2linked(num: int) -> ListNode: if not num: return ListNode(0) curr = None while num: node = ListNode(num % 10) if curr is not None: node.next = curr curr = node num //= 10 return curr
def removeNthFromEnd2(head: ListNode, n: int) -> ListNode: dummy = ListNode(-1) dummy.next = head node, i, dictionary = dummy, 0, {} while node is not None: dictionary[i] = node i = i + 1 node = node.next # print(i) prev = dictionary[i - 1 - n] prev.next = prev.next.next return dummy.next # cannot return head as it can be removed
def test(n1: List[int], n2: List[int], s1: int, s2: int) -> None: h1 = ListNode.make(n1[: s1]) t1 = h1 while t1.next is not None: t1 = t1.next h2 = ListNode.make(n2[: s2]) t2 = h2 while t2.next is not None: t2 = t2.next h3 = ListNode.make(n1[s1:]) t1.next = t2.next = h3 # print(h1, h2) print(getIntersectionNode(h1, h2))
def removeNthFromEnd(head: ListNode, n: int) -> ListNode: dummy = ListNode(-1) dummy.next = head node, i = dummy, 0 while node is not None: i = i + 1 node = node.next n = i - n - 1 # convert to nth from head node = dummy for i in range(n): node = node.next node.next = node.next.next return dummy.next # cannot return head as it can be removed
def get_linked(nums: List[int], pos: int) -> ListNode: n, dummy = len(nums), ListNode(-1) curr = dummy temp = tail = None for i in range(n): curr.next = ListNode(nums[i]) curr = curr.next if pos == i: temp = curr if i == n - 1: tail = curr if pos >= 0: tail.next = temp return dummy.next
def removeElements(self, head: ListNode, val: int) -> ListNode: newNode = ListNode(head.val) newTmpNode = newNode tmpNode = head while tmpNode.next != None: if tmpNode.val != val: newTmpNode.next = ListNode(tmpNode.val) newTmpNode = newTmpNode.next tmpNode = tmpNode.next if tmpNode.val != val: if tmpNode.val != val: newTmpNode.next = ListNode(tmpNode.val) newTmpNode = newTmpNode.next if val == newNode.val: return newNode.next return newNode.next
def addTwoNumbers2(self,l1,l2): carry = 0 root = n = ListNode(0) while l1 or l2 or carry: v1 = v2 = 0 if l1: v1 = l1.val l1 = l1.next if l2: v2 = l2.val l2 = l2.next carry, val = divmod(v1 + v2 + carry, 10) n.next = ListNode(val) n = n.next return root.next
def mergeTwoLists2(l1: ListNode, l2: ListNode) -> ListNode: nums = [] for head in [l1, l2]: while head is not None: insort(nums, head.val) head = head.next return ListNode.make(nums)
def mergeKLists(lists: List[ListNode]) -> ListNode: nums = [] for head in lists: while head is not None: insort(nums, head.val) head = head.next return ListNode.make(nums)
def test(nums: List[int], val: int) -> None: head = ListNode.make(nums) node = head while node is not None: if node.val == val: break node = node.next else: return deleteNode(node) print(head)
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: # Baseline t o(n), s o(n) summation = ListNode(0) head = summation carry = 0 while (l1 is not None or l2 is not None): n1 = 0 if (l1 is not None): n1 = l1.value() l1 = l1.next() n2 = 0 if (l2 is not None): n2 = l2.value() l2 = l2.next() summation.next = ListNode((n1 + n2 + carry) % 10) carry = (n1 + n2) // 10 summation = summation.next if (carry > 0): summation.next = ListNode(carry) return (head.next)
def addTwoNumbers(l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy = ListNode(0) pivot = dummy carry = 0 while l1 or l2: v1 = l1.val if l1 else 0 v2 = l2.val if l2 else 0 carry, val = divmod(v1 + v2 + carry, 10) pivot.next = ListNode(val) pivot = pivot.next l1 = l1.next if l1 else None l2 = l2.next if l2 else None if carry: pivot.next = ListNode(carry) return dummy.next
def mergeKLists(lists): """ :type lists: List[ListNode] :rtype: ListNode """ import heapq my_queue = [] for sll in lists: head = sll while head: heapq.heappush(my_queue, head.val) head = head.next my_queue.sort() if my_queue: head = ListNode(my_queue[0]) cur = head for i in range(1, len(my_queue)): tmp = ListNode(my_queue[i]) cur.next = tmp cur = tmp return head
def reverseBetween(head, m, n): if not head: return head dummy = ListNode(-1) dummy.next = head m_prev = dummy for i in range(m - 1): m_prev = m_prev.next m_cur = m_prev.next rev_head = m_cur for i in range(n - m): next_ele = m_cur.next next_link = next_ele.next next_ele.next = rev_head rev_head = next_ele m_cur.next = next_link m_prev.next = rev_head return dummy.next
def partition(head, x): """ :type head: ListNode :type x: int :rtype: ListNode """ less = ListNode(-1) eg = ListNode(-1) pivot = head less_pivot = less eg_pivot = eg while pivot: if pivot.val < x: less_pivot.next = pivot less_pivot = less_pivot.next if pivot.val >= x: eg_pivot.next = pivot eg_pivot = eg_pivot.next pivot = pivot.next eg_pivot.next = None less_pivot.next = eg.next return less.next
def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head1=l1 head2=l2 root =n= ListNode(0) carry=0 while head1 or head2 or carry: val1 = val2 = 0 if head1: val1 = head1.val head1=head1.next if head2: val2 = head2.val head2=head2.next carry,val = divmod(val1+val2+carry,10) # n.val = val n.next=ListNode(val) n=n.next return root.next
def deleteDuplicates(head): """ :type head: ListNode :rtype: ListNode """ dmh = ListNode(-1) temp = dmh while head: if head.next and head.val == head.next.val: x = head.val while head and head.val == x: head = head.next else: temp.next = head head = head.next temp = temp.next temp.next = None return dmh.next
def oddEvenList(head: ListNode) -> ListNode: if head is None: return None prev = head node = head.next h2 = ListNode(-1) even = h2 i = 0 while node is not None: if not i % 2: prev.next = node.next even.next = node even = node else: prev = node node = node.next i += 1 even.next = None prev.next = h2.next return head
from listNode import ListNode class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ if not head.next: return slow = fast = head for i in range(n): fast = fast.next if not fast: head = head.next return head while fast.next: fast = fast.next slow = slow.next slow.next = slow.next.next return head node = ListNode(1) node.next = ListNode(2) result = Solution().removeNthFromEnd(node, 2) print(result.val)
def main(): print(removeDuplicateNodes(ListNode.make([]))) # None print(removeDuplicateNodes(ListNode.make([1, 2, 3, 3, 2, 1]))) # [1, 2, 3] print(removeDuplicateNodes(ListNode.make([1, 1, 1, 1, 2]))) # [1, 2]
def main(): print(removeNthFromEnd(ListNode.make([1, 2, 3, 4, 5]), 2)) # 1->2->3->5 print(removeNthFromEnd(ListNode.make([1, 2]), 2)) # 2 print(removeNthFromEnd(ListNode.make([1]), 1)) # None
def addTwoNumbers2(self,l1,l2): carry = 0 root = n = ListNode(0) while l1 or l2 or carry: v1 = v2 = 0 if l1: v1 = l1.val l1 = l1.next if l2: v2 = l2.val l2 = l2.next carry, val = divmod(v1 + v2 + carry, 10) n.next = ListNode(val) n = n.next return root.next def printList(self,l1): node = l1 while node: print(node.val,end=',') node=node.next l1 = ListNode(2) l1.next = ListNode(4) l1.next.next = ListNode(3) l2= ListNode(5) l2.next = ListNode(6) l2.next.next = ListNode(4) lyst = Solution().addTwoNumbers(l1,l2) Solution().printList(lyst)
class Solution: def reverseList(self, head: ListNode) -> ListNode: tmpNode = head newNode = None while tmpNode != None: newFlagListNode = ListNode(tmpNode.val) newFlagListNode.next = newNode newNode = newFlagListNode tmpNode = tmpNode.next return newNode s = Solution() head1 = ListNode(1) head2 = ListNode(2) head3 = ListNode(3) head4 = ListNode(4) head46 = ListNode(6) head5 = ListNode(5) head6 = ListNode(6) head1.next = head2 head2.next = head3 head3.next = head4 head4.next = head46 head46.next = head5 head5.next = head6 print(head1) newListNode = s.reverseList(head1) print(newListNode)
def main(): # test A = ListNode('A') B = ListNode('B') C = ListNode('C') D = ListNode('D') E = ListNode('E') F = ListNode('F') G = ListNode('G') A.next, B.next, C.next, D.next, E.next, F.next = B, C, D, E, F, G G.next = B # print(A.traverse()) #this will enter a loop when linked list has a cycle print(has_cycle(A)) print(has_cycle_inplace(A))
def main(): # test A = ListNode('A') B = ListNode('B') C = ListNode('C') D = ListNode('D') E = ListNode('E') F = ListNode('F') G = ListNode('G') H = ListNode('H') I = ListNode('I') A.next, B.next, C.next, D.next = B, C, D, E # E.next, F.next = F, D print(A.traverse()) print(E.traverse())
def main(): print(reverseList(ListNode.make([1, 2, 3, 4, 5]))) # 5->4->3->2->1