# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ curr = head while curr and curr.next: if curr.val != curr.next.val: curr = curr.next else: curr.next = curr.next.next return head # import import sys, os sys.path.insert(0, os.path.abspath((os.pardir + os.sep) * 2 + 'mylib')) from LinkedList import ListNode, generate_linked_list, print_linked_list # test node = generate_linked_list([1, 2, 3, 3, 3, 4, 4, 5]) node = Solution().deleteDuplicates(node) print_linked_list(node)
# self.val = x # self.next = None class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy = cur = ListNode(None) 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 # import import sys, os sys.path.insert(0, os.path.abspath((os.pardir + os.sep) * 2 + 'mylib')) from LinkedList import ListNode, generate_linked_list, print_linked_list # test a = generate_linked_list([2, 3, 5]) b = generate_linked_list([1, 4, 6]) print_linked_list(Solution().mergeTwoLists(a, b))
from LinkedList import create_linked_list, print_linked_list ll = create_linked_list(4, lambda x: x) print_linked_list(ll) def swap(prev, s1): pass
else: small_list.add(value) else: if large_list == None: large_list = Node(value) else: large_list.add(value) concat(small_list, large_list) return small_list def concat(linkedList1, linkedList2): last_node_linkedList1 = linkedList1.get(linkedList1.getLength() - 1) first_node_linkedList2 = linkedList2.get(0) last_node_linkedList1.nex = first_node_linkedList2 last_node_linkedList1.nex.prev = last_node_linkedList1 linkedList = Node(5) linkedList.add(1) linkedList.add(8) linkedList.add(0) linkedList.add(3) linkedList.add(7) linkedList.add(2) linkedList.add(9) print_linked_list(linkedList) pivot = 7 sorted_linkedList = sort_from_pivot(linkedList, pivot) print_linked_list(sorted_linkedList)
while rev: # cannot use slow, doesn't work if len = 1 if rev.val != tail.val: isPalin = False # don't break the loop and reverse the first half again # rev must be updated the same time as slow since slow.next # changes rev.next slow, slow.next, rev = rev, slow, rev.next tail = tail.next return isPalin # import import sys, os sys.path.insert(0, os.path.abspath((os.pardir + os.sep) * 2 + 'mylib')) from LinkedList import ListNode, generate_linked_list, print_linked_list # test node = generate_linked_list([1,2,3,2,1]) print(Solution().isPalindrome(node)) print_linked_list(node) # original node is destructed node = generate_linked_list([1,2,3,2,1]) print(Solution().isPalindrome2(node)) print_linked_list(node) # original node is restored print(Solution().isPalindrome(generate_linked_list([1,2,3,1]))) print(Solution().isPalindrome2(generate_linked_list([1,2,3,1]))) print(Solution().isPalindrome(generate_linked_list([1]))) print(Solution().isPalindrome2(generate_linked_list([1])))
return dummy.next # using heaps - pyhton heapq is minimun heap # O(nlogk) where n = total elements, k = number of lists # heappush and heappop takes logK time, and maintain heap invariant # we only maintain heap size equal to k, not n ! def mergeKLists2(self, lists): from heapq import heappush, heappop heap = [] for node in lists: if node: # check empty list heappush(heap, (node.val, node)) # min heap based on node.val dummy = cur = ListNode(None) while heap: val, node = heappop(heap) # pop the min item cur.next = node cur = cur.next if node.next: heappush(heap, (node.next.val, node.next)) return dummy.next # import import sys, os sys.path.insert(0, os.path.abspath((os.pardir + os.sep) * 2 + 'mylib')) from LinkedList import ListNode, generate_linked_list, print_linked_list # test a = generate_linked_list([2,3,5]) b = generate_linked_list([1,4,6]) print_linked_list(Solution().mergeKLists([a, b])) print_linked_list(Solution().mergeKLists2([a, b]))