Пример #1
0
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        # Input: 1->2->3->4->5->NULL, m = 2, n = 4
        # Output: 1->4->3->2->5->NULL
        if m == n or head is None or head.next is None:
            return head

        pre = ListNode(0)
        pre.next = head

        cnt = 0
        p = pre
        tmp1 = p.next
        while cnt < n - 1:
            if m - 1 <= cnt:
                if tmp1.next:
                    tmp2 = p.next
                    p.next = tmp1.next
                    tmp1.next = p.next.next
                    p.next.next = tmp2
                else:
                    pass
            else:
                p = p.next
                tmp1 = p.next
            cnt += 1
            # print_nodes(pre)
        return pre.next
Пример #2
0
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head is None:
            return head

        start_point = ListNode(0)
        start_point.next = head

        start, end = start_point, head
        past = head.val
        p = head.next
        while p:
            now = p.val
            if now != past:
                past = now
                start = end
                end = p
                p = p.next
            else:
                p = p.next
                while p:
                    if p.val == past:
                        p = p.next
                    else:
                        break
                end = p
                p = start.next
                start.next = end
                while p != end:
                    tmp = p.next
                    del p
                    p = tmp
                p = start.next
                end = start
        return start_point.next
Пример #3
0
def create_list(nums):
    L = ListNode(0)
    l = L
    for i in nums:
        l.next = ListNode(i)
        l = l.next
    return L.next
Пример #4
0
def create_linked_list(l):
    size = len(l)
    prev_node, curr_node = None, None
    for i in range(0, size):
        index = size - i - 1
        curr_node = ListNode(l[index])
        curr_node.next = prev_node
        prev_node = curr_node
    return prev_node
Пример #5
0
 def swapPairs(self, head: ListNode) -> ListNode:
 	l = ListNode(0)
 	l.next = head
 	p, q = l, l.next
 	while p.next != None and q.next != None:
 		r = q.next
 		p.next.next = q.next.next
 		s = p.next
 		r.next = s
 		p.next = r
 		p = p.next.next
 		q = q.next
 	return l.nextjiao
Пример #6
0
 def deleteDuplicates(self, head: ListNode) -> ListNode:
     p = ListNode(0)
     p.next = head
     head, q = p, p
     while p.next != None and p.next.next != None:
         if p.next.val == p.next.next.val:
             while p.next.next != None and p.next.val == p.next.next.val:
                 p = p.next
             q.next = p.next.next
         else:
             q = q.next
         p = p.next
     return head.next
Пример #7
0
def create_cyclic_linked_list(l, cycle_val):
    size = len(l)
    prev_node, curr_node, cyclic_node, tail = None, None, None, None
    for i in range(0, size):
        index = size - i - 1
        curr_node = ListNode(l[index])
        if i == 0:
            tail = curr_node
        curr_node.next = prev_node
        prev_node = curr_node
        if l[index] == cycle_val:
            cyclic_node = curr_node
    if tail != None and cyclic_node != None:
        tail.next = cyclic_node
    return prev_node
Пример #8
0
 def sortedListToBST(self, head: ListNode) -> TreeNode:
     if head == None:
         return None
     p = ListNode(0)
     p.next = head
     head = p
     p1, p2 = head, head
     while p2.next != None and p2.next.next != None:
         p1 = p1.next
         p2 = p2.next.next
     root = TreeNode(p1.next.val)
     q = p1.next.next
     p1.next = None
     root.left = self.sortedListToBST(head.next)
     root.right = self.sortedListToBST(q)
     return root
Пример #9
0
 def sortList(self, head: ListNode) -> ListNode:
     if head == None or head.next == None:
         return head
     low, fast = head, head.next
     while fast != None and fast.next != None:
         low = low.next
         fast = fast.next.next
     p = low.next
     low.next = None
     l1 = self.sortList(head)
     l2 = self.sortList(p)
     l = ListNode(0)
     q = l
     while l1 != None and l2 != None:
         if l1.val < l2.val:
             q.next = l1
             l1 = l1.next
         else:
             q.next = l2
             l2 = l2.next
         q = q.next
     if l1 != None:
         q.next = l1
     if l2 != None:
         q.next = l2
     return l.next
 def reverseList(self, head: ListNode) -> ListNode:
     if head is None or head.next is None:
         return head
     second_node = head.next
     new_head = self.reverseList(second_node)
     second_node.next = head
     head.next = None
     return new_head
Пример #11
0
 def partition(self, head: ListNode, x: int) -> ListNode:
 	p = ListNode(0)
 	p.next = head
 	head = p
 	flag = False
 	while p.next != None:
 		if p.next.val >= x and flag == False:
 			q = p 
 			flag = True
 			p = p.next
 		elif p.next.val < x and flag == True:
 			r = p.next
 			p.next = p.next.next
 			r.next = q.next
 			q.next = r 
 			q = q.next
 		else:
 			p = p.next
 	return head.next
Пример #12
0
 def insertionSortList(self, head: ListNode) -> ListNode:
 	if head == None:
 		return None
 	p = ListNode(0)
 	p.next = head
 	head = p
 	sort, p = head, head.next
 	while p.next != None:
 		if p.val < p.next.val:
 			p = p.next
 			continue
 		r = sort
 		while p.next.val > r.next.val:
 			r = r.next
 		if r.next != p.next:
 			q = p.next
 			p.next = p.next.next
 			q.next = r.next
 			r.next = q
 	return sort.next
Пример #13
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     l = ListNode(0)
     p, q, r = l1, l2, l
     while p != None and q != None:
         if p.val < q.val:
             r.next = p
             p = p.next
         else:
             r.next = q
             q = q.next
         r = r.next
     r.next = p if p != None else q
     return l.next
Пример #14
0
    def partition(self, head: ListNode, x: int) -> ListNode:
        pre = ListNode(0)
        pre.next = head
        past = pre
        curr, end_list = head, head
        flag = False
        while curr:
            if curr.val >= x:
                flag = True
                end_list = curr
            else:
                if flag is False:
                    past = curr
                else:
                    tmp = past.next
                    past.next = curr
                    end_list.next = curr.next
                    curr.next = tmp

                    past = past.next
            curr = curr.next
    
        return pre.next
Пример #15
0
	def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
	    flag1, flag2 = 0, 0
	    L = l1
	    L_prev = None
	    while l1 != None and l2 != None:
	    	L_prev = l1 if L_prev==None else L_prev.next
	    	flag2 = (l1.val + l2.val + flag1) // 10
	    	l1.val = (l1.val + l2.val + flag1) % 10
	    	flag1 = flag2
	    	l1 = l1.next
	    	l2 = l2.next
	    if l2 != None:
	    	l1 = l2
	    	L_prev.next = l1
	    while l1 != None:
	    	flag2 = (l1.val + flag1) // 10
	    	l1.val = (l1.val + flag1) % 10
	    	flag1 = flag2
	    	l1 = l1.next
	    	L_prev = L_prev.next
	    if flag1 == 1:
	    	L_prev.next = ListNode(1)
	    return L	    
Пример #16
0
def merge(l1, l2):
    # both lists empty
    if l1 == None and l2 == None:
        return None
    result = ListNode(None)
    curr = result
    while l1 != None or l2 != None:
        if l1 == None:
            curr.next = ListNode(l2.val)
            l2 = l2.next
        elif l2 == None:
            curr.next = ListNode(l1.val)
            l1 = l1.next
        else:
            # both lists have values
            # add the greater value
            if l1.val <= l2.val:
                curr.next = ListNode(l1.val)
                l1 = l1.next
            else:
                curr.next = ListNode(l2.val)
                l2 = l2.next
        curr = curr.next
    return result.next
 def reverseListIterative(self, head: ListNode) -> ListNode:
     if head is None or head.next is None:
         return head
     # There are at least 2 nodes
     curr_node = head.next
     prev_node = head
     while curr_node.next is not None:
         # At least 3 nodes
         next_node = curr_node.next
         curr_node.next = prev_node
         prev_node = curr_node
         curr_node = next_node
     # Finish up by letting the last node in the list point at its predecessor
     curr_node.next = prev_node
     # The original head is the last node now
     head.next = None
     return curr_node