# 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))
# @param B : head node of linked list # @return the head node in the linked list # whenever A or B or carry is not empty, create a new node def addTwoNumbers(self, A, B): dummy = node = ListNode(None) carry = 0 while A or B or carry: a_val = A.val if A else 0 b_val = B.val if B else 0 total = a_val + b_val + carry carry, cur_val = divmod(total, 10) node.next = ListNode(cur_val) node = node.next if A: A = A.next if B: B = B.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, 4, 3]) b = generate_linked_list([5, 6, 4]) node = Solution().addTwoNumbers(a, b) print_linked_list(node)
start = slow = fast = head while fast and fast.next and fast.next.next: slow = slow.next fast = fast.next.next if fast == slow: hasCycle = True break if hasCycle: # slow and start with meet at cycle entry again while start != slow: start, slow = start.next, slow.next return start else: return None # 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, 4]) print(Solution().detectCycle(node)) # link node 4 to node 3 node.next.next.next.next = node.next.next print(Solution().detectCycle(node).val) # link node 1 to itself node.next = node print(Solution().detectCycle(node).val)
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])))
# @param A : head node of linked list # @param B : integer # @return the head node in the linked list # put everything smaller than B to the left # put everything greater/equl than B to the right # join left and right but don't fogrget to terminate right. def partition(self, A, B): dummy_left = cur_left = ListNode(None) dummy_right = cur_right = ListNode(None) while A: if A.val < B: cur_left.next = A cur_left = cur_left.next else: cur_right.next = A cur_right = cur_right.next A = A.next cur_right.next = None # end of right might not be empty! cur_left.next = dummy_right.next return dummy_left.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 node = generate_linked_list([1,4,3,2,5,2]) node = Solution().partition(node, 3) print_linked_list(node)