def isPalindrome(linkedList): stack = LinkedList() res = True slow = linkedList.head fast = linkedList.head count=0 while(fast!= None and fast.next !=None): count += 1 stack.push(Node(slow.data)) slow = slow.next fast = fast.next.next if(fast != None): slow = slow.next while(slow!= None): val1 = slow.data val2 = stack.pop() if(val1 != val2): res = False break slow = slow.next return res
def addDigits(l1, l2): result, carry = None, 0 if (l1.next or l2.next): result, carry = addDigits(l1.next, l2.next) if (not result): result = LinkedList() sum = l1.data + l2.data + carry print(f"{l1.data} + {l2.data} + {carry} = {sum}") result.push(Node(sum % 10)) carry = sum // 10 return result, carry
def sumDigitsReverseOrder(l1, l2): node1 = l1.head node2 = l2.head count, carry = 0, 0 result = LinkedList() while (not (node1 == None and node2 == None)): val1 = node1.data if node1 else 0 val2 = node2.data if node2 else 0 sum = val1 + val2 + carry result.appendNode(Node(sum % 10)) carry = sum // 10 if (node1): node1 = node1.next if (node2): node2 = node2.next if (carry): result.appendNode(Node(carry)) return result
node = linkedList.head while (node != None): sum += node.data * (10**count) node = node.next count += 1 return sum def sumDigitsReverseOrder(l1, l2): n1 = convertLinkedListToNumber(l1) n2 = convertLinkedListToNumber(l2) print(f"{n1} + {n2} = {n1 + n2}") linkedList1 = LinkedList() linkedList1.appendNode(Node(7)) linkedList1.appendNode(Node(1)) linkedList1.appendNode(Node(6)) linkedList2 = LinkedList() linkedList2.appendNode(Node(5)) linkedList2.appendNode(Node(9)) linkedList2.appendNode(Node(2)) print("Input: ") linkedList1.printList() linkedList2.printList() print("Output: ") sumDigitsReverseOrder(linkedList1, linkedList2)
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"
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 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"
isLoopPresent = True break start = linkedList.head if isLoopPresent: while(start != slow): start = start.next slow = slow.next return slow else: return None linkedList = LinkedList() linkedList.appendNode(Node("A")) linkedList.appendNode(Node("B")) n1 = Node("C") linkedList.appendNode(n1) linkedList.appendNode(Node("D")) linkedList.appendNode(Node("E")) linkedList.appendNode(n1) print("Input: ") # linkedList.printList() # do not print list since it contains a loop -> program will stuck in infinite loop # A -> B -> C -> D -> E -> C print("Output: ") res = hasLoop(linkedList)
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"
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"
import os sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) from Util.Node import Node from Util.LinkedList import LinkedList def deleteMiddleNode(node): if(node == None or node.next == None): return False node.data = node.next.data node.next = node.next.next return True linkedList = LinkedList() linkedList.appendNode(Node(1)) testNode = Node(2) linkedList.appendNode(testNode) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(5)) print("Input: ") linkedList.printList() print("Output: ") # deleteDups(linkedList.head) deleteMiddleNode(testNode) linkedList.printList()
# 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"
for i in range(0, diff): node1 = node1.next elif (len2 > len1): for i in range(0, diff): node2 = node2.next while (node1 != None and node2 != None): if (node1 == node2): return node1 node1 = node1.next node2 = node2.next return None linkedList1 = LinkedList() linkedList1.appendNode(Node(3)) linkedList1.appendNode(Node(1)) linkedList1.appendNode(Node(5)) linkedList1.appendNode(Node(9)) linkedList2 = LinkedList() linkedList2.appendNode(Node(4)) linkedList2.appendNode(Node(6)) n1 = Node(7) linkedList1.appendNode(n1) linkedList2.appendNode(n1) n2 = Node(2) linkedList1.appendNode(n2)
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())
def partition(linkedList, x): previous = linkedList.head current = linkedList.head.next while (current != None): if (current.data < x): previous.next = current.next temp = current #can avoid creating temp variable but it makes it simpler to understand temp.next = linkedList.head linkedList.head = temp current = previous.next else: previous = previous.next current = current.next linkedList = LinkedList() linkedList.appendNode(Node(3)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(8)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(10)) linkedList.appendNode(Node(2)) linkedList.appendNode(Node(1)) x = 5 print("Input: ") linkedList.printList() print(f"x: {x}") print("Output: ")
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())
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())
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"
current = current.next def deleteDupsWithoutBuffer(head): current = head while(current != None): runner = current while(runner!=None and runner.next != None): if(runner.next.data == current.data): runner.next = runner.next.next runner = runner.next current = current.next linkedList = LinkedList() linkedList.appendNode(Node(1)) linkedList.appendNode(Node(1)) linkedList.appendNode(Node(2)) linkedList.appendNode(Node(3)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(4)) linkedList.appendNode(Node(5)) linkedList.appendNode(Node(5)) print("Input: ") linkedList.printList() print("Output: ") # deleteDups(linkedList.head) deleteDupsWithoutBuffer(linkedList.head)
# 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"
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