else: node.next = tail tail = node node = nextNode head2 = tail head = None tail = None count = 0 while True: if count % 2 == 1 and head2: tail.next = head2 head2 = head2.next tail = tail.next elif count % 2 == 0 and head1: if head == None: head = head1 tail = head else: tail.next = head1 tail = tail.next head1 = head1.next else: break count += 1 return head vals = [1,2,3,4,5,6] head1 = ListNode.build(vals) print(Solution().reorderList(head1))
if headNode == None: headNode = l1 if preNode != None: preNode.next = l1 preNode = l1 l1 = l1.next preNode.next = None else: if l1.val < l2.val: if headNode == None: headNode = l1 if preNode != None: preNode.next = l1 preNode = l1 l1 = l1.next preNode.next = None else: if headNode == None: headNode = l2 if preNode != None: preNode.next = l2 preNode = l2 l2 = l2.next preNode.next = None return headNode # @lc code=end print(Solution().mergeTwoLists(ListNode.build([1, 2, 4]), ListNode.build([1, 3, 4])))
node.next = lastNode lastNode = node if nodeNext != None: node = nodeNext else: head = node break return head # return None if head == None else reverse(None, head) # stack = [] # while head != None: # node = head # ndoeNext = head.next # node.next = None # stack.append(node) # head = ndoeNext # head = None # lastNode = None # while len(stack) > 0: # node = stack.pop() # if head == None: # head = node # lastNode = node # else: # lastNode.next = node # lastNode = node # return head # @lc code=end print(Solution().reverseList(ListNode.build([1,2,3,4,5])))
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None from typing import List import sys sys.path.append('..') import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) from Tool.Python.ListNode import ListNode class Solution: def reversePrint(self, head: ListNode) -> List[int]: arr = [] while head != None: arr.append(head.val) head = head.next l, r = 0, len(arr) - 1 while l < r: val = arr[l] arr[l] = arr[r] arr[r] = val l = l + 1 r = r - 1 return arr node = ListNode.build([1, 2, 3, 4]) print(node)
# @lc code=start # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def sortList(self, head: ListNode) -> ListNode: nodes = [] node = head while node: nodes.append(node) node = node.next nodes.sort() # 用排序算法控制在O(n log n) res = None head = None for node in nodes: node.next = None if head == None: head = node res = node else: res.next = node res = res.next return head # @lc code=end print(Solution().sortList(ListNode.build([4, 2, 1, 3])))
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) from Tool.Python.ListNode import ListNode class Solution: def rotateRight(self, head: ListNode, k: int) -> ListNode: if head == None: return head prevewNode = head count = 1 while prevewNode.next != None: prevewNode = prevewNode.next count = count + 1 prevewNode.next = head node = head retainCount = count - k % count while retainCount > 0: prevewNode = node node = node.next retainCount = retainCount - 1 head = node prevewNode.next = None return head print(Solution().rotateRight(ListNode.build([1, 2, 3, 4, 5]), 1)) print(Solution().rotateRight(ListNode.build([0, 1, 2]), 3)) print(Solution().rotateRight(ListNode.build([1, 2]), 2)) # print(Solution().rotateRight(None, 0))
if l1: stack1.append(l1.val) l1 = l1.next if l2: stack2.append(l2.val) l2 = l2.next r = ListNode(val=0) while True: if len(stack1) > 0: r.val += stack1.pop() if len(stack2) > 0: r.val += stack2.pop() if len(stack1) == 0 and len(stack2) == 0: if r.val > 9: r.val -= 10 r = ListNode(val=1, next=r) break else: if r.val > 9: r.val -= 10 r = ListNode(val=1, next=r) else: r = ListNode(val=0, next=r) return r # @lc code=end print(Solution().addTwoNumbers(ListNode.build([7, 2, 4, 3]), ListNode.build([5, 6, 4])))
class Solution: def reverseKGroup(self, head: ListNode, k: int) -> ListNode: root, pre_node = None, ListNode() l_node = head r_node = head count = 0 while r_node != None: if count < k - 1: r_node = r_node.next count = count + 1 else: pre_node.next, pre_node_next, r_node_next = r_node, l_node, r_node.next while l_node != r_node: node = l_node l_node = l_node.next node.next = None node.next = r_node.next r_node.next = node if root == None: root = pre_node.next pre_node = pre_node_next l_node, r_node = r_node_next, r_node_next count = 0 return root # @lc code=end print(Solution().reverseKGroup(ListNode.build([1, 2, 3, 4, 5]), 1))
import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) from Tool.Python.ListNode import ListNode class Solution: def getKthFromEnd(self, head: ListNode, k: int) -> ListNode: leftNode, rightNode = head, head while k > 1: if rightNode.next == None: return None else: k -= 1 rightNode = rightNode.next while rightNode.next: leftNode = leftNode.next rightNode = rightNode.next return leftNode print(Solution().getKthFromEnd(ListNode.build([1]), 1))
# def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ nodes, node = [], head while node: nodes.append(node) node = node.next left, right, direction = 0, len(nodes) - 1, 0 while left < right: l_node = nodes[left] r_node = nodes[right] direction += 1 if direction % 2 == 1: l_node.next = r_node left += 1 else: r_node.next = l_node right -= 1 if left == right: nodes[left].next = None return head # @lc code=end print(Solution().reorderList(ListNode.build([1, 2, 3, 4, 5])))
node, isDuplicate = head, False while node != None and node.next != None: if node.val != node.next.val: if isDuplicate == False: if _headNode == None: _head = ListNode(node.val) _headNode = _head else: _headNode.next = ListNode(node.val) _headNode = _headNode.next node, isDuplicate = node.next, False else: node, isDuplicate = node.next, True if isDuplicate == False: if _head == None: _head = node if _headNode == None: _headNode = node else: _headNode.next = node _headNode = _headNode.next pass return _head # @lc code=end print(Solution().deleteDuplicates(None)) print(Solution().deleteDuplicates(ListNode.build([1, 2, 2]))) print(Solution().deleteDuplicates(ListNode("1->2->3->3->4->4->5".split("->")))) print(Solution().deleteDuplicates(ListNode("1->1->1->2->3".split("->"))))
class Solution: def partition(self, head: ListNode, x: int) -> ListNode: pre_left, left, right = None, head, head while right and right.next: right = right.next head, foot = None, None while left != right: leftNext = left.next if left.val >= x: left.next = None if foot == None: right.next = left foot = left right = right.next else: foot.next = left foot = foot.next if pre_left != None: pre_left.next = leftNext else: pre_left = left if head == None: head = left left = leftNext if head == None: head = left return head # @lc code=end print(Solution().partition(ListNode.build([1,2]),3))
# # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # class Solution: # def mergeKLists(self, lists: [ListNode]) -> ListNode: # import heapq # dummy = ListNode(0) # p = dummy # head = [] # for i in range(len(lists)): # if lists[i] : # heapq.heappush(head, (lists[i].val, i)) # lists[i] = lists[i].next # while head: # val, idx = heapq.heappop(head) # p.next = ListNode(val) # p = p.next # if lists[idx]: # heapq.heappush(head, (lists[idx].val, idx)) # lists[idx] = lists[idx].next # return dummy.next # Solution().mergeKLists([[1], [1,3,4], [2,6]]) # Solution().mergeKLists([ListNode.build([1]), ListNode.build([1,3,4]), ListNode.build([2,6])]) Solution().mergeKLists( [ListNode.build([]), ListNode.build([1, 3, 4]), ListNode.build([2, 6])])
while True: if index == m: if pre_node: pre_node.next = None swap_pre_node, swap_node, swap_index = None, node, index while swap_node and swap_index <= n: next_node = swap_node.next next_pre_node = swap_node next_pre_node.next = None if swap_pre_node: swap_node.next = swap_pre_node swap_pre_node = next_pre_node swap_node = next_node swap_index += 1 if swap_node: node.next = swap_node if pre_node: pre_node.next = swap_pre_node if not pre_node: head = swap_pre_node break index += 1 pre_node = node node = node.next return head # @lc code=end print(Solution().reverseBetween(ListNode.build([1, 2, 3, 4, 5]), 1, 8))
# self.next = next # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def sortedListToBST(self, head: ListNode) -> TreeNode: def sortedArrToBST(arr: []) -> TreeNode: if len(arr) == 0: return None midIndex = len(arr) // 2 midVal = arr[midIndex] leftVals = arr[:midIndex] rightVals = arr[midIndex + 1:] return TreeNode(midVal, left=sortedArrToBST(leftVals), right=sortedArrToBST(rightVals)) vals = [] while head: vals.append(head.val) head = head.next return sortedArrToBST(vals) # @lc code=end head = ListNode.build([-10, -3, 0, 5, 9]) ans = Solution().sortedListToBST(head) print(ans)