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 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 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(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 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 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 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 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 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 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 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 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 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 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
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 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 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(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
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)
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)
''' 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)
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
def test_remove_multiple(self): head = ListNode.build_linked_list([1, 2, 6, 3, 4, 5, 6]) expect = ListNode.build_linked_list([1, 2, 3, 4, 5]) self.assertEqual(self.solution.remove_elements(head, 6), expect)
def test_remove_no_element(self): head = ListNode.build_linked_list([1, 2, 3, 4]) self.assertEqual(self.solution.remove_elements(head, 5), head)
def test_remove_tail(self): head = ListNode.build_linked_list([1, 2, 3, 4]) expect = ListNode.build_linked_list([1, 2, 3]) self.assertEqual(self.solution.remove_elements(head, 4), expect)
def test_remove_same(self): head = ListNode.build_linked_list([1, 1, 1]) self.assertIsNone(self.solution.remove_elements(head, 1))