:rtype: ListNode """ pre = ListNode(0) right = head while right is not None: temp = right.next # temp saves the next node of right right.next = None # disconnect "right" with the next # search from pre, and 'left" will stop at the node before the correct insertion position left = pre while left.next is not None and left.next.val < right.val: left = left.next # insert the 'right' node next = left.next left.next = right right.next = next # advance the 'right' iterator right = temp return pre.next obj = Solution() l1 = ListNode(0) l1.fromList([8, 1, 5, 6, 7, 2, 4, 3]) PrintLinkedList(l1) l2 = obj.insertionSortList(l1) PrintLinkedList(l2)
def __init__(self, head): """ @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. :type head: ListNode """ self.head = head def getRandom(self): """ Returns a random node's value. :rtype: int """ i, node, res = 2, self.head.next, self.head.val while node: j = randrange(0, i) if j == 0: res = node.val i += 1 node = node.next return res l1 = ListNode(0) l1.fromList([8, 1, 3]) obj = Solution(l1) print(obj.getRandom()) # Your Solution object will be instantiated and called as such: # obj = Solution(head) # param_1 = obj.getRandom()
l1 = l1.next l2 = l2.next it = it.next head = reverse(helper.next) node, carry = head, 0 while node: temp = carry carry = (node.val + carry) // 10 node.val = ( node.val + temp ) % 10 # bug fixed: cannot use updated carry to calculate the remain value, must save carry first to "temp" node = node.next head = reverse(head) if carry > 0: helper.val = carry helper.next = head return helper else: return head l1, l2 = ListNode(0), ListNode(0) l1.fromList([9, 9, 9, 9]) l2.fromList([1]) PrintLinkedList(l1) PrintLinkedList(l2) l3 = Solution().addTwoNumbers(l2, l1) PrintLinkedList(l3)
node = fakeHead # iterator for step in range(1, n + 1): node = node.next if step == m - 1: pre = node if step == n: tail = node # get the head (mth node) head = pre.next pre.next = None # disconnect pre and this section of list temp = tail.next # temporarily save tail.next to be connected in future tail.next = None # disconnect tail with temp # reverse the partial list pre1, node = None, head while node: t = node.next node.next = pre1 pre1 = node node = t # connect pre with the new head (previous tail) pre.next = tail # connect new tail (previous head) with temp head.next = temp return fakeHead.next obj = Solution() l1 = ListNode(0) l1.fromList([1, 2, 3]) l2 = obj.reverseBetween(l1, 1, 3) PrintLinkedList(l2)
helper = ListNode(0) helper.next = head pre, tail = helper, helper count = 0 while tail: tail = tail.next count += 1 if ( count == k ) and tail: # bug fixed --- need to reconsider tail if it has been updated. consider the case 1->2->None with k = 3 nextHead = tail.next # temporarily save the next group's head (newHead, newTail) = reverseKGroup2(head, tail, k) # swap this k group nodes pre.next = newHead # link the pre node with the new head newTail.next = nextHead # link the new tail with the previously saved next group's head pre = newTail # update pre node head = nextHead # update head node tail = newTail # update tail node count = 0 return helper.next l1 = ListNode(0) l1.fromList([1, 2, 3, 4, 5]) PrintLinkedList(l1) print() l2 = reverseKGroup(l1, 3) PrintLinkedList(l2) print() print(CountLinkedList(l2))
:type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) node = head while l1 or l2: if not l1: node.next = l2 break elif not l2: node.next = l1 break else: if l1.val < l2.val: node.next = l1 node = l1 l1 = l1.next else: node.next = l2 node = l2 l2 = l2.next return head.next l1, l2 = ListNode(0), ListNode(0) l1.fromList([1, 3, 5, 7, 9]) l2.fromList([2, 4, 6, 8]) l3 = merge(l2, l1) PrintLinkedList(l3)
i += 1 # ith node from the head tail = tail.next if i >= n: # bug fixed here. old statement "i > n" pre = pre.next # link pre to the node after the removed node node = pre.next pre.next = node.next return head.next test_cases = [[1, 2, 3, 4, 5]] for test_case in test_cases: head = ListNode(0) head.fromList(test_case) PrintLinkedList(head) print() result = removeNthFromEnd(head, 2) PrintLinkedList(result) print() head = ListNode(0) head.fromList(test_case) result = removeNthFromEnd(head, 5) PrintLinkedList(result) print() head = ListNode(0) head.fromList(test_case) result = removeNthFromEnd(head, 1) PrintLinkedList(result) print()
fast = head while slow != fast: slow = slow.next fast = fast.next return slow # 2nd round solution on 6/11/2019 def detectCycle2(self, head): fast, slow = head, head while fast and fast.next: fast = fast.next.next slow = slow.next if fast == slow: break if fast is None or fast.next is None: return None fast = head while fast != slow: fast = fast.next slow = slow.next return slow obj = Solution() l1 = ListNode(0) l1.fromList([1]) print(obj.detectCycle2(l1))
if not fast: count *= 2 right = slow else: count = 2 * count + 1 right = slow.next # reverse the right half nodes (if odd length, not including the mid node) cur, pre = right, None while cur: next = cur.next cur.next = pre pre = cur cur = next right = pre # parallel comparison for i in range(count // 2): if head.val != right.val: return False else: # bug fixed: forgot to advance pointers head = head.next right = right.next return True obj = Solution() l1 = ListNode(0) l1.fromList([1, 2, 2, 1]) print(obj.isPalindrome(l1))
def reverse(head): if head is None: return head node = head.next head.next = None # break the link between head and the second node while node: temp = node.next node.next = head head = node node = temp return head left = head right = reverse(slow) # Now merge the two linked lists while right: # bug fixed: should not use "while left != slow" because length: left >= right tempL, tempR = left.next, right.next left.next = right right.next = tempL left = tempL right = tempR obj = Solution() l1 = ListNode(0) l1.fromList([0, 1, 2, 3]) PrintLinkedList(l1) print(obj.reorderList2(l1)) PrintLinkedList(l1)
class Solution: # see onenote def oddEvenList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return None dummy = ListNode(0) dummy.next = head odd, even = head, dummy while even and even.next: even.next = odd.next even = even.next if even and even.next: odd.next = even.next odd = odd.next odd.next = dummy.next return head obj = Solution() head = ListNode(0) head.fromList([1, 2, 3, 4, 5]) PrintLinkedList(obj.oddEvenList(head))
pre.next = head tail, l = pre, 0 while tail.next is not None and l < k: # When while condition fails, tail is either on "tail" node, or l == k, which means tail is on the ready to move position l += 1 tail = tail.next if tail.next is None: # if tail is on "tail", this means k >= l, so we need to put the tail back on k%l position k = k % l tail = pre for i in range(k): tail = tail.next helper = pre while tail.next is not None: # After this, helper's next will point to the start node of the section to be rotated to the beginning helper = helper.next tail = tail.next tail.next = pre.next pre.next = helper.next helper.next = None return pre.next obj = Solution() l1 = ListNode(0) l1.fromList([1, 2]) l2 = obj.rotateRight(l1, 2001) PrintLinkedList(l2)
class Solution: def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ pre = ListNode(0) pre.next = head left, right = pre, head while right: if right.val != val: left.next = right left = right right = right.next left.next = None return pre.next obj = Solution() test_case = [1, 1] l1 = ListNode(0) l1.fromList(test_case) l1.printAll() print() l2 = obj.removeElements(l1, 1) l2.printAll()
left = right right = right.next if not right: return head # Now right's value is x, and left is the node "pre" right. # "left" will link to the next available node whose value is smaller than x. "right" will be the new head of the right part # After traversing all nodes, we need to relink "left" and "right" pre, node = right, right.next while node: if node.val < x: # put node to left because it is less than x # link node to left part left.next = node left = node # process the right part pre.next = node.next else: pre = node node = node.next # relink the left and right left.next = right return helper.next obj = Solution() l1 = ListNode(0) l1.fromList([6,4,3,2,5,2]) l2 = obj.partition(l1,3) PrintLinkedList(l2)
while node: n += 1 node = node.next res, node = [], root q, r = divmod(n, k) for _ in range(k): length = q if r: length += 1 r -= 1 if not length: res.append(None) else: res.append(node) for _ in range(length - 1): node = node.next temp = node.next node.next = None node = temp return res head = ListNode(0) head.fromList([1, 2, 3, 4]) res = Solution().splitListToParts(head, 5) for node in res: PrintLinkedList(node)