if head is None or head.next is None: return head larger_head, equal_head, smaller_head = ListNode(0), ListNode(0), ListNode(0) larger, equal, smaller, cur = larger_head, equal_head, smaller_head, head while cur: if cur.val > k: larger.next = cur larger = larger.next elif cur.val == k: equal.next = cur equal = equal.next else: smaller.next = cur smaller = smaller.next cur = cur.next smaller.next = equal_head.next equal.next = larger_head.next larger.next = None return smaller_head.next if __name__ == "__main__": l1 = LinkedList(10, 1, 6) l1.display() t = Solution() l2 = t.listPivoting(l1.getHead(), 4) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
# Time: O(n) # Space: O(1) def evenOddMerge(self, head): if head is None or head.next is None: return head odd_head, even_head, cur = ListNode(0), ListNode(0), head odd, even = odd_head, even_head count = 0 while cur: if count == 1: odd.next = cur odd = odd.next else: even.next = cur even = even.next cur = cur.next count = 1 - count even.next = odd_head.next odd.next = None return even_head.next if __name__ == "__main__": l1 = LinkedList(10, 1, 100) l1.display() t = Solution() l2 = t.evenOddMerge(l1.getHead()) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
class Solution: # Time: O(n) # Space: O(1) def isLinkedListAPalindrome(self, head): if head is None or head.next is None: return True fast, slow, prev = head, head, None while fast and fast.next: fast = fast.next.next prev = slow slow = slow.next prev.next = None prev, cur = None, slow while cur: next = cur.next cur.next = prev prev = cur cur = next while prev and head: if prev.val != head.val: return False head = head.next prev = prev.next return True if __name__ == "__main__": l1 = LinkedList(4, 1, 2) l1.display() t = Solution() print t.isLinkedListAPalindrome(l1.getHead())
prev, cur = None, slow.next slow.next = None while cur: next = cur.next cur.next = prev prev = cur cur = next dummy = ListNode(0) cur = dummy while head or prev: if head: cur.next = head head = head.next cur = cur.next if prev: cur.next = prev prev = prev.next cur = cur.next return dummy.next if __name__ == "__main__": l1 = LinkedList(10, 1, 100) l1.display() t = Solution() l2 = t.zippingLinkedList(l1.getHead()) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
class Solution: # Time: O(len(F) + len(L)) # Space: O(1) def mergeTwoSortedLists(self, F, L): dummy = ListNode(0) cur, l1, l2 = dummy, F, L while l1 and l2: if l1.val < l2.val: cur.next = l1 cur, l1 = cur.next, l1.next else: cur.next = l2 cur, l2 = cur.next, l2.next if l1 is None: cur.next = l2 else: cur.next = l1 return dummy.next if __name__ == "__main__": l1 = LinkedList(5, True) l2 = LinkedList(5, True) l1.display() l2.display() t = Solution() l3 = t.mergeTwoSortedLists(l1.getHead(), l2.getHead()) cur = l3 while cur: print cur.val, cur = cur.next print "\n"
from Util.LinkedList import ListNode from Util.LinkedList import LinkedList class Solution: # Time: O(n): # Space: O(1): def removeKthLastList(self, head, k): dummy = ListNode(0) dummy.next = head fast = dummy for i in range(k): fast = fast.next slow = dummy while fast and fast.next: fast, slow = fast.next, slow.next slow.next = slow.next.next return dummy.next if __name__ == "__main__": l1 = LinkedList(5) l1.display() t = Solution() l2 = t.removeKthLastList(l1.getHead(), 3) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
from Util.LinkedList import ListNode from Util.LinkedList import LinkedList class Solution: # Time: O(n) # Space: O(1) def reverseLinkedList(self, head): if head is None or head.next is None: return head prev, cur = None, head while cur: next = cur.next cur.next = prev prev = cur cur = next return prev if __name__ == "__main__": l1 = LinkedList(10) l1.display() t = Solution() l2 = t.reverseLinkedList(l1.getHead()) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
from Util.LinkedList import ListNode from Util.LinkedList import LinkedList class Solution: # Time: O(n) # Space: O(1) def findMedianSortedCircularLinkedList(self, node): cur, start, length = node.next, node, 1 while cur != node: if start.val <= cur.val: start = cur cur = cur.next length += 1 start = start.next for i in range((length - 1)/2): start = start.next if length % 2 == 1: return start.val return 0.5 * (start.val + start.next.val) if __name__ == "__main__": l1 = LinkedList(12, 1, 9, True) l1.display() l1.createCycle(0) t = Solution() print t.findMedianSortedCircularLinkedList(l1.getHead())
# Space: O(1) def reverseSublistSF(self, head, s, f): dummy = ListNode(0) dummy.next = head lastUnSwap, cur, diff = dummy, head, f - s while cur and s > 1: cur = cur.next lastUnSwap = lastUnSwap.next s -= 1 firstSwap, prev = cur, lastUnSwap while cur and diff >= 0: next = cur.next cur.next = prev prev = cur cur = next diff -= 1 lastUnSwap.next = prev firstSwap.next = cur return dummy.next if __name__ == "__main__": l1 = LinkedList(5) l1.display() t = Solution() l2 = t.reverseSublistSF(l1.getHead(), 2, 4) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
def addTwoNumbers(self, l1, l2): dummy = ListNode(0) carry, cur = 0, dummy while l1 or l2: sum = carry if l1: sum += l1.val l1 = l1.next if l2: sum += l2.val l2 = l2.next carry = sum / 10 sum %= 10 cur.next = ListNode(sum) cur = cur.next if carry == 1: cur.next = ListNode(1) return dummy.next if __name__ == "__main__": l1 = LinkedList(4, 1, 9) l1.display() l2 = LinkedList(4, 1, 9) l2.display() t = Solution() l3 = t.addTwoNumbers(l1.getHead(), l2.getHead()) cur = l3 while cur: print cur.val, cur = cur.next print "\n"
slow = slow.next if fast == slow: slow = head while slow != fast: fast, slow = fast.next, slow.next return slow return None def overlappingNoCycleLists(self, l1, l2): h1, h2, len1, len2 = l1, l2, 0, 0 while h1: h1, len1 = h1.next, len1 + 1 while h2: h2, len2 = h2.next, len2 + 1 h1, h2 = l1, l2 if len1 > len2: for i in range(len1 - len2): h1 = h1.next else: for i in range(len2 - len1): h2 = h2.next while h1 and h2 and h1 != h2: h1, h2 = h1.next, h2.next return h1 if __name__ == "__main__": l1 = LinkedList(8) l1.createCycle(3) l2 = LinkedList(4) cur = l2.getHead() while cur.next: cur = cur.next cur.next = l1.getHead().next t = Solution() print t.overlappingLists(l1.getHead(), l2.getHead())
from Util.LinkedList import LinkedList class Solution: # Time: O(n) # Space: O(1) def cyclicallyRightShiftList(self, head, k): if head is None or head.next is None: return head cur = head length = 1 while cur.next: cur = cur.next length += 1 k = length - k%length cur.next = head for i in range(k): cur = cur.next newHead = cur.next cur.next = None return newHead if __name__ == "__main__": l1 = LinkedList(5, 1, 5) l1.display() t = Solution() l2 = t.cyclicallyRightShiftList(l1.getHead(), 3) cur = l2 while cur: print cur.val, cur = cur.next print "\n"
from Util.LinkedList import ListNode from Util.LinkedList import LinkedList class Solution: # Time: O(n) # Space: O(1) def checkingCycle(self, head): if head is None: return None fast, slow = head, head while fast and fast.next: fast = fast.next.next slow = slow.next if slow == fast: slow = head while slow != fast: slow = slow.next fast = fast.next return slow return None if __name__ == "__main__": l1 = LinkedList(6) l1.createCycle(3) t = Solution() c = t.checkingCycle(l1.getHead()) print c.val if c else -1