コード例 #1
0
def main():
    # test
    A = ListNode('A')
    B = ListNode('B')
    C = ListNode('C')
    D = ListNode('D')
    E = ListNode('E')
    F = ListNode('F')
    G = ListNode('G')

    A.next, B.next, C.next, D.next, E.next, F.next = B, C, D, E, F, G
    G.next = B
    # print(A.traverse()) #this will enter a loop when linked list has a cycle

    print(has_cycle(A))
    print(has_cycle_inplace(A))
コード例 #2
0
ファイル: reverseList.py プロジェクト: zcg741/leetcode
 def reverseList(self, head: ListNode) -> ListNode:
     tmpNode = head
     newNode = None
     while tmpNode != None:
         newFlagListNode = ListNode(tmpNode.val)
         newFlagListNode.next = newNode
         newNode = newFlagListNode
         tmpNode = tmpNode.next
     return newNode
コード例 #3
0
def num2linked(num: int) -> ListNode:
    if not num:
        return ListNode(0)
    curr = None
    while num:
        node = ListNode(num % 10)
        if curr is not None:
            node.next = curr
        curr = node
        num //= 10
    return curr
コード例 #4
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
   # Baseline t o(n), s o(n)
   summation = ListNode(0)
   head = summation
   carry = 0
   while (l1 is not None or l2 is not None):
     n1 = 0
     if (l1 is not None):
       n1 = l1.value()
       l1 = l1.next()
     n2 = 0  
     if (l2 is not None):
       n2 = l2.value()
       l2 = l2.next()
     summation.next = ListNode((n1 + n2 + carry) % 10)
     carry = (n1 + n2) // 10
     summation = summation.next
   if (carry > 0):
     summation.next = ListNode(carry)
   return (head.next)
コード例 #5
0
def removeNthFromEnd2(head: ListNode, n: int) -> ListNode:
    dummy = ListNode(-1)
    dummy.next = head
    node, i, dictionary = dummy, 0, {}
    while node is not None:
        dictionary[i] = node
        i = i + 1
        node = node.next
    # print(i)
    prev = dictionary[i - 1 - n]
    prev.next = prev.next.next
    return dummy.next  # cannot return head as it can be removed
コード例 #6
0
def removeNthFromEnd(head: ListNode, n: int) -> ListNode:
    dummy = ListNode(-1)
    dummy.next = head
    node, i = dummy, 0
    while node is not None:
        i = i + 1
        node = node.next
    n = i - n - 1  # convert to nth from head
    node = dummy
    for i in range(n):
        node = node.next
    node.next = node.next.next
    return dummy.next  # cannot return head as it can be removed
def main():
	# test
	A = ListNode('A')
	B = ListNode('B')
	C = ListNode('C')
	D = ListNode('D')
	E = ListNode('E')
	F = ListNode('F')
	G = ListNode('G')
	H = ListNode('H')
	I = ListNode('I')
	A.next, B.next, C.next, D.next = B, C, D, E
	# E.next, F.next = F, D
	print(A.traverse())
	print(E.traverse())
コード例 #8
0
def reverseBetween(head, m, n):
    if not head:
        return head
    dummy = ListNode(-1)
    dummy.next = head

    m_prev = dummy
    for i in range(m - 1):
        m_prev = m_prev.next

    m_cur = m_prev.next
    rev_head = m_cur
    for i in range(n - m):
        next_ele = m_cur.next
        next_link = next_ele.next
        next_ele.next = rev_head
        rev_head = next_ele
        m_cur.next = next_link

    m_prev.next = rev_head
    return dummy.next
コード例 #9
0
ファイル: No19RemoveNthNode.py プロジェクト: Qiujm/MyPython
from listNode import ListNode


class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        if not head.next:
            return
        slow = fast = head
        for i in range(n):
            fast = fast.next
        if not fast:
            head = head.next
            return head

        while fast.next:
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return head


node = ListNode(1)
node.next = ListNode(2)
result = Solution().removeNthFromEnd(node, 2)
print(result.val)
コード例 #10
0
ファイル: reverseList.py プロジェクト: zcg741/leetcode
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        tmpNode = head
        newNode = None
        while tmpNode != None:
            newFlagListNode = ListNode(tmpNode.val)
            newFlagListNode.next = newNode
            newNode = newFlagListNode
            tmpNode = tmpNode.next
        return newNode


s = Solution()
head1 = ListNode(1)
head2 = ListNode(2)
head3 = ListNode(3)
head4 = ListNode(4)
head46 = ListNode(6)
head5 = ListNode(5)
head6 = ListNode(6)
head1.next = head2
head2.next = head3
head3.next = head4
head4.next = head46
head46.next = head5
head5.next = head6
print(head1)
newListNode = s.reverseList(head1)
print(newListNode)
コード例 #11
0
ファイル: No2AddTwoNumbers.py プロジェクト: Qiujm/MyPython
    def addTwoNumbers2(self,l1,l2):
        carry = 0
        root = n = ListNode(0)
        while l1 or l2 or carry:
            v1 = v2 = 0
            if l1:
                v1 = l1.val
                l1 = l1.next
            if l2:
                v2 = l2.val
                l2 = l2.next
            carry, val = divmod(v1 + v2 + carry, 10)
            n.next = ListNode(val)
            n = n.next
        return root.next

    def printList(self,l1):
        node = l1
        while node:
            print(node.val,end=',')
            node=node.next

l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)
l2= ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
lyst = Solution().addTwoNumbers(l1,l2)
Solution().printList(lyst)
コード例 #12
0
def deleteNode(node: ListNode):  # the node cannot be last one, so we can
    node.val = node.next.val  # set next node value to this one
    node.next = node.next.next  # then remove next node, this is tricky