def reverseList(self, head: ListNode) -> ListNode: if not head: return None head_1, head_2 = ListNode(0), ListNode(0) head_1.next = head while head_1.next: node = head_1.next head_1.next = node.next node.next = head_2.next head_2.next = node return head_2.next
def loop(l1, l2): if not l1: return l2 elif not l2: return l1 if l1.val <= l2.val: res = ListNode(l1.val) res.next = loop(l1.next, l2) return res elif l1.val > l2.val: res = ListNode(l2.val) res.next = loop(l1, l2.next) return res
def partition(self, head: ListNode, x: int) -> ListNode: p0, p1 = ListNode(0), ListNode(0) head0, head1 = p0, p1 while head is not None: if head.val < x: p0.next = head p0 = p0.next else: p1.next = head p1 = p1.next head = head.next p0.next = head1.next p1.next = None return head0.next
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode': if head is None: return None p = ListNode(0) p.next = head head = p while p.next is not None and p.next.next is not None: if p.next.val == p.next.next.val: current = p.next.val while p.next.next is not None and p.next.next.val == current: p.next.next = p.next.next.next p.next = p.next.next else: p = p.next return head.next
def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if head is None or head.next is None or m == n: return head head, p = ListNode(0), head head.next = p p, q, r = head, head.next, head.next.next n -= m m -= 1 while m > 0: p, q = q, r if r is not None: r = r.next m -= 1 flag1, flag2 = p, q n += 1 while n > 0: q.next = p n -= 1 if n == 0: flag1.next, flag2.next = q, r break p, q = q, r if r is not None: r = r.next return head.next
def insertionSortList(self, head): if not head or not head.next: return head dummy_head = ListNode(-1) dummy_head.next = head pre = dummy_head cur = dummy_head while cur.next: if cur.next.val < pre.next.val: pre = dummy_head while pre.next.val < cur.next.val: pre = pre.next if pre != cur: # insert tmp after pre tmp = cur.next cur.next = tmp.next tmp.next = pre.next pre.next = tmp else: cur = cur.next return dummy_head.next
def reverseKGroup(self, head, k): """ :type head: ListNode :type k: int :rtype: ListNode """ # Get Length p = head ll = 0 while p is not None: p = p.next ll += 1 # reverse p = head head = ListNode(0) head.next = p prev_end = head for i in range(0, ll // k): cur_start = cur_end = prev_end.next for j in range(0, k - 1): cur_end = cur_end.next if cur_end is None: next_start = None else: next_start = cur_end.next p, q, r = prev_end, prev_end.next, prev_end.next.next for j in range(0, k): q.next = p p, q = q, r if r is not None: r = r.next cur_start.next = next_start prev_end.next = cur_end prev_end = cur_start return head.next
def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ node_1, node_2 = ListNode(None), head node_1.next = head head = node_1 for i in range(1, n): node_2 = node_2.next while node_2.next: node_1 = node_1.next node_2 = node_2.next node_1.next = node_1.next.next return head.next
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(None) dummy.next = head prev, cur = dummy, head i = 1 while i<m: prev, cur = cur, cur.next i += 1 head, tail = prev, cur while i<=n: next_node = cur.next cur.next = prev prev, cur = cur, next_node i += 1 head.next = prev tail.next = cur return dummy.next
def swapPairs(self, head: ListNode) -> ListNode: if not head: return head cur = ListNode(None) cur.next = head head = cur while True: if cur.next: node_1 = cur.next if node_1.next: node_2 = node_1.next cur.next = node_2 node_1.next = node_2.next node_2.next = node_1 cur = node_1 else: break else: break return head.next
def removeElements(self, head: ListNode, val: int) -> ListNode: dummy = ListNode(None) dummy.next = head pre, cur = dummy, head while cur: if cur.val == val: cur = cur.next pre.next = cur else: pre = cur cur = cur.next return dummy.next
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(None) dummy.next = head i = 1 head = dummy while i<m: head = head.next i += 1 node = head.next while i<n: node = node.next i+=1 tail = node.next node.next = None node = head.next head.next = tail while node: temp = node.next node.next = head.next head.next = node node = temp return dummy.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ p = head head = ListNode(0) head.next = p p = head while p.next is not None and p.next.next is not None: tp1, tp2, q = p.next, p.next.next, p.next.next.next p.next, tp2.next, tp1.next = tp2, tp1, q p = tp1 return head.next
def deleteDuplicates(self, head: ListNode) -> ListNode: dummy = ListNode(None) dummy.next = head pre, cur = dummy, head while cur and cur.next: if cur.val == cur.next.val: node = cur.next.next while node and node.val == cur.val: node = node.next pre.next = node cur = node else: pre, cur = cur, cur.next return dummy.next
def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ p = head head = ListNode(0) head.next = p p = head while p is not None and p.next is not None: while p.next is not None and p.next.val == val: p.next = p.next.next p = p.next return head.next
def removeNthFromEnd(self, head, n): dummy = ListNode(-1) dummy.next = head pre = dummy cur = head end = head for i in range(1, n): end = end.next while end.next: pre = pre.next cur = cur.next end = end.next pre.next = cur.next return dummy.next
def make_cycle_list(L: list, pos) -> ListNode: """ Almost make a circle list """ node = ListNode(L[0]) res = node pointer = 1 count = 1 # Add next node while count < 3 * len(L): node.next = ListNode(L[pointer]) node = node.next if pointer % len(L) == len(L) - 1: pointer = pos else: pointer += 1 count += 1 return res
def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ # get length p = head ll = 0 while p is not None: ll += 1 p = p.next on = ll + 1 - n # delete node p = ListNode(0) p.next = head q = p for _ in range(1, on): q = q.next q.next = q.next.next return p.next
def isPalindrome(self, head: ListNode) -> bool: dummy1, dummy2 = ListNode(None), ListNode(None) dummy1.next = head slow = fast = dummy1 while fast and fast.next: slow = slow.next fast = fast.next.next cur = slow.next while cur: node = cur.next cur.next = dummy2.next dummy2.next = cur cur = node slow, fast = dummy1, dummy2 while slow and fast: if slow.val==fast.val: slow = slow.next fast = fast.next else: return False else: return True
from data_structure import ListNode class Solution: def hasCycle(self, head: ListNode) -> bool: p, q = head, head while q is not None: p = p.next q = q.next if q.next is None else q.next.next if q is None: return False if p == q: return True return False if __name__ == "__main__": # Example 1 n0, n1, n2, n3 = ListNode(3), ListNode(2), ListNode(0), ListNode(-4) n0.next, n1.next, n2.next, n3.next = n1, n2, n3, n1 print(Solution().hasCycle(n0)) # Example 2 n0, n1 = ListNode(1), ListNode(2) n0.next, n1.next = n1, n0 print(Solution().hasCycle(n0)) # Example 3 n0 = ListNode(1) print(Solution().hasCycle(n0))
if a is b: found = True break # distance from head to loop head is the same as meeting piont to loop head # find the loop head by moving a and h if found: h = head while h is not a: a = a.next h = h.next return h return None else: return None t = ListNode(3) t.next = ListNode(2) t.next.next = ListNode(0) t.next.next.next = ListNode(-4) t.next.next.next.next = t.next #t = ListNode(1) #t.next = ListNode(2) #t.next.next = t sol = Solution() print sol.detectCycle(t).val