def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) curr = head carry = 0 while True: if l1 != None: carry += l1.val l1 = l1.next if l2 != None: carry += l2.val l2 = l2.next curr.val = carry % 10 carry /= 10 if l1 != None or l2 != None or carry != 0: # Good! add a new node to add wi curr.next = ListNode(0) curr = curr.next else: break return head
def partition(self, head, x): """ :type head: ListNode :type x: int :rtype: ListNode """ head_smaller = ListNode(0) head_bigger = ListNode(0) p_smaller = head_smaller p_bigger = head_bigger current = head while current: if current.val < x: p_smaller.next = current current = current.next p_smaller = p_smaller.next p_smaller.next = None else: p_bigger.next = current current = current.next p_bigger = p_bigger.next p_bigger.next = None p_smaller.next = head_bigger.next return head_smaller.next
def addTwoNumbers_2(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if not (l1 and l2): return None if not l1: return l2 if not l2: return l1 result = ListNode(-1) h = result curry = 0 while l1 or l2: v1 = l1.val if l1 else 0 v2 = l2.val if l2 else 0 sum = v1 + v2 + curry h.next = ListNode(sum % 10) curry = sum / 10 if l1: l1 = l1.next if l2: l2 = l2.next h = h.next if curry > 0: h.next = ListNode(curry) return result.next
def generate_list(nums): head = ListNode(0) curr = head for i in xrange(len(nums)): curr.next = ListNode(nums[i]) curr = curr.next return 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 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 merge(self, l1, l2): dummy = cur = ListNode(0) while l1 and l2: if l1.val <= l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next cur.next = l1 or l2 return dummy.next
def mergeTwoLists(l1, l2): dummy_head = ListNode(0) tail = dummy_head while l1 and l2: if l1.val < l2.val: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next tail = tail.next if l1: tail.next = l1 else: tail.next = l2 return dummy_head.next
def swap_nodes_in_pairs_wrong(head): if head is None: return None new_head = ListNode(0) tail = new_head while head and head.next: tail.next = head.next tail.next.next = head # head.next.next is head!!! tail = tail.next.next head = head.next.next if head: tail.next = head return new_head.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 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 swap_nodes_in_pairs(head): if head is None: return None new_head = ListNode(0) tail = new_head while head and head.next: next_start = head.next.next tail.next = head.next tail.next.next = head head.next = next_start tail = tail.next.next head = head.next if head: tail.next = head return new_head.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return feakhead = ListNode(-1) prev = feakhead prev.next = head while head and head.next: next = head.next.next prev.next = head.next prev.next.next = head head.next = next prev = prev.next.next head = prev.next return feakhead.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
while fast_point and slow_point and fast_point.next: fast_point = fast_point.next.next slow_point = slow_point.next if fast_point == slow_point: break return (fast_point != None) and (fast_point.next != None) # solution 2 reverse cycle linked list will have same head def hasCycle(head): if head and head.next and head == reverseList(head): return True return False def reverseList(head): before = after = None while head: after = head.next head.next = before before = head head = after return before list_node = ListNode(0) print has_cycle_O1(list_node)
''' 237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. ''' from data_structure.list_node import ListNode def delete_node(node): node.val = node.next.val node.next = node.next.next head = ListNode(0)
v1 = l1[i1] v2 = l2[i2] if v1 < v2: new_list.append(v1) i1 += 1 elif v1 > v2: new_list.append(v2) i2 += 1 else: new_list.append(v1) new_list.append(v2) i1 += 1 i2 += 1 if (len(l1) > comm_length): new_list += l1[comm_length:] if (len(l2) > comm_length): new_list += l2[comm_length:] return new_list node1 = ListNode(1) node2 = ListNode(1) print merge_sorted_lists(node1, node2) list1 = [1, 5, 7, 10] list2 = [2, 4, 9, 13, 20, 30] print merge_sorted_lists_wrong(list1, list2)
return feakhead = ListNode(-1) prev = feakhead prev.next = head while head and head.next: next = head.next.next prev.next = head.next prev.next.next = head head.next = next prev = prev.next.next head = prev.next return feakhead.next head = ListNode(1) curr_node = head curr_node.next = ListNode(2) curr_node = curr_node.next curr_node.next = ListNode(3) curr_node = curr_node.next curr_node.next = ListNode(4) new_head = swap_nodes_in_pairs(head) while new_head: print new_head.val new_head = new_head.next