예제 #1
0
 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
예제 #2
0
 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
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
 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
예제 #6
0
    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
예제 #7
0
 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
예제 #8
0
파일: 19.py 프로젝트: yanpan9/Some-Code
    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
예제 #9
0
파일: 92.py 프로젝트: yanpan9/Some-Code
 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
예제 #10
0
 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
예제 #11
0
 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
예제 #12
0
파일: 92.py 프로젝트: yanpan9/Some-Code
 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
예제 #13
0
 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
예제 #14
0
파일: 82.py 프로젝트: yanpan9/Some-Code
 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
예제 #15
0
 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
예제 #18
0
 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
예제 #19
0
파일: 234.py 프로젝트: yanpan9/Some-Code
 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
예제 #20
0
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))
예제 #21
0
                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