def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode # """ pre = ListNode(0) dummy = pre carry = 0 while l1 and l2: digit = (l1.val + l2.val + carry)%10 carry = (l1.val + l2.val + carry)/10 pre.next = ListNode(digit) pre = pre.next l1 = l1.next l2 = l2.next tail = None if l1: tail = l1 if l2: tail = l2 while tail: digit = (tail.val + carry)%10 carry = (tail.val + carry)/10 pre.next = ListNode(digit) pre = pre.next tail = tail.next if carry: pre.next = ListNode(1) return dummy.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ # if head is None: # return # if head.next is None: # return head if head is None or head.next is None: return head last = ListNode(0) newHead = head.next cur = head while cur and cur.next: next = cur.next.next last.next = cur.next cur.next.next = cur cur.next = None last = cur cur = next if next is not None and next.next is None: last.next = next return newHead
def oddEvenList(self, head): """ :type head: ListNode :rtype: ListNode """ odd = ListNode(0) odd_head = odd even = ListNode(0) even_head = even isOdd = True while head: if isOdd: odd.next = head odd = odd.next isOdd = False else: even.next = head even = even.next isOdd = True head = head.next if isOdd: odd.next = None else: even.next = None odd.next = even_head.next return odd_head.next
def reverseBetween_9(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head head = dummy for i in range(1, m): # get previous N head = head.next # revert node N to node M, node previous N and node post M need be keep pre_m_node = head m_node = head.next n_node = m_node post_n_node = m_node.next for i in range(n-m): tmp = post_n_node.next post_n_node.next = n_node n_node = post_n_node post_n_node = tmp m_node.next = post_n_node pre_m_node.next = n_node return dummy.next
def reverseBetween_9(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head head = dummy for i in range(1, m): # get previous N head = head.next # revert node N to node M, node previous N and node post M need be keep pre_m_node = head m_node = head.next n_node = m_node post_n_node = m_node.next for i in range(n - m): tmp = post_n_node.next post_n_node.next = n_node n_node = post_n_node post_n_node = tmp m_node.next = post_n_node pre_m_node.next = n_node return dummy.next
def remove_elements(self, head: ListNode, val: int) -> ListNode: sentinel = ListNode(0) sentinel.next = head previous, current = sentinel, head while current: if current.val == val: previous.next = current.next else: previous = current current = current.next return sentinel.next
def swapPairs_dummy(self, head): # Write your code here if head == None or head.next == None: return head dummy = ListNode(0); dummy.next = head p = dummy while p.next and p.next.next: tmp = p.next.next p.next.next = tmp.next tmp.next = p.next p.next = tmp p = p.next.next return dummy.next
def removeNthFromEnd(self, head, n): dummyNode= ListNode(10) dummyNode.next = head fast = dummyNode while n>0: fast = fast.next n -=1 # print fast.val slow = dummyNode while fast.next: slow = slow.next fast = fast.next slow.next = slow.next.next return dummyNode.next
def mergeTwoLists(self, l1, l2): if not l1 and not l2: return None if not l2: return l1 elif not l1: return l2 res = ListNode(0) head = res while l1 and l2: if l1.val > l2.val: res.next = l2 l2 = l2.next else: res.next = l1 l1 = l1.next res = res.next res.next = l1 or l2 return head.next
def insertionSortList(self, head): if not head: return head dummy = ListNode(0) dummy.next = head curr = head while curr.next: if curr.next.val < curr.val: pre = dummy while pre.next.val < curr.next.val: pre = pre.next tmp = curr.next curr.next = tmp.next tmp.next = pre.next pre.next = tmp else: curr = curr.next return dummy.next
def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ if head is None: return head dummyNode = ListNode(0) dummyNode.next = head p = dummyNode while p.next: if p.next.val == val: p.next = p.next.next else: p = p.next return dummyNode.next
def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ feak_head = ListNode(0) feak_head.next = head slow = feak_head fast = head for i in range(0, n): fast = fast.next while fast != None: fast = fast.next slow = slow.next slow.next = slow.next.next return feak_head.next
def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head dummy = ListNode(0) dummy.next = head prev = dummy while head and head.next: if head.val == head.next.val: val = head.val while head and val == head.val: head = head.next prev.next = head else: head = head.next prev = prev.next return dummy.next
def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head head1 = dummy for i in range(m - 1): head1 = head1.next p = head1.next for i in range(n - m): tmp = head1.next head1.next = p.next p.next = p.next.next head1.next.next = tmp return dummy.next
def removeNthFromEnd_my(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ if head is None: return len = 0 dummyNode = ListNode(0) dummyNode.next = head p1 = dummyNode while p1: p1 = p1.next len += 1 n = len - n -1 p1 = dummyNode if n == 0: return head.next while n > 0: p1 = p1.next n -= 1 p1.next = p1.next.next return head