before = None pre = None current = head while current: if current.val < x and pre and pre.val >= x: if not before: before = dummy nxt = current.next pre.next = nxt current.next = before.next before.next = current before = current current = nxt else: if current.val < x: before = current pre = current current = current.next return dummy.next if __name__ == '__main__': s = Solution() # 头节点比X小的情况 print(listnode_to_list(s.partition(create_listnode([1, 4, 3, 2, 5, 2]), 3))) # 头节点比X大的情况 print(listnode_to_list(s.partition(create_listnode([4, 3, 1, 2, 5, 2]), 3)))
from ListNodeHelper import ListNode, create_listnode, listnode_to_list class Solution: # 方法一, 遍历, 72ms def removeElements(self, head: ListNode, val: int) -> ListNode: """ 通常链表操作都会添加一个哨兵节点,能显著降低边界条件判断的复杂度 """ dummy = ListNode(None) dummy.next = head pre = dummy current = head while current: if current.val == val: pre.next = current.next else: pre = pre.next current = current.next return dummy.next if __name__ == '__main__': s = Solution() print( listnode_to_list( s.removeElements(create_listnode([1, 2, 6, 3, 4, 5, 6]), 6))) print( listnode_to_list( s.removeElements(create_listnode([6, 1, 2, 6, 3, 4, 5]), 6)))
stack = [] while head: stack.append(head) head = head.next current = dummy while stack: current.next = stack.pop() current = current.next current.next = None return dummy.next # 方法二,原地反转,40ms def reverseList(self, head: ListNode) -> ListNode: dummy = ListNode(None) dummy.next = head while head and head.next: nxt = head.next head.next = nxt.next nxt.next = dummy.next dummy.next = nxt return dummy.next if __name__ == '__main__': s = Solution() print(listnode_to_list(s.reverseList(create_listnode([1, 2, 3, 4, 5])))) # 空链表 print(listnode_to_list(s.reverseList(create_listnode([])))) # 单节点链表 print(listnode_to_list(s.reverseList(create_listnode([1]))))
如果不得使用临时缓冲区,该怎么解决? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicate-node-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ from ListNodeHelper import ListNode, create_listnode, listnode_to_list class Solution: def removeDuplicateNodes(self, head: ListNode) -> ListNode: nodes = set() root = ListNode(None) root.next = head pre = root while head: if head.val in nodes: head = head.next pre.next = head else: nodes.add(head.val) head = head.next pre = pre.next return root.next if __name__ == '__main__': s = Solution() print(listnode_to_list(s.removeDuplicateNodes(create_listnode([1, 2, 3, 3, 2, 1])))) print(listnode_to_list(s.removeDuplicateNodes(create_listnode([1, 1, 1, 1, 2]))))
root1, root2, root3 = ListNode(0), ListNode(0), ListNode(0) head1, head2, head3 = root1, root2, root3 while head: if head.val < x: head1.next = head head1 = head1.next elif head.val > x: head2.next = head head2 = head2.next elif head.val == x: head3.next = head head3 = head3.next head = head.next if root2.next: head1.next = root2.next head2.next = root3.next else: head1.next = root3.next head3.next = None return root1.next if root1.next else root3.next if __name__ == '__main__': s = Solution() print(listnode_to_list(s.partition(create_listnode([1, 4, 3, 2, 5, 2]), 3))) print(listnode_to_list(s.partition(create_listnode([1]), 0))) print(listnode_to_list(s.partition(create_listnode([1]), 3))) print(listnode_to_list(s.partition(create_listnode([1]), 1))) print(listnode_to_list(s.partition(create_listnode([1, 2]), 2)))
while headB: if headB in nodes_A: return headB headB = headB.next return None class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: """ 这题应该是比较明显的双指针题,要是能实现一种算法让两个指针分别从A和B点往C点走,两个指针分别走到C后,又各自从另外一个指针的起点,也就是A指针第二次走从B点开始走,B指针同理,这样,A指针走的路径长度 AO + OC + BO 必定等于B指针走的路径长度 BO + OC + AO,这也就意味着这两个指针第二轮走必定会在O点相遇,相遇后也即到达了退出循环的条件,代码如下: 作者:ceamanz 链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/solution/shuang-zhi-zhen-zou-liang-bian-zou-dao-di-er-bian-/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 """ ta, tb = headA, headB while ta != tb: ta = ta.next if ta else headB tb = tb.next if tb else headA return tb if __name__ == '__main__': s = Solution() print( s.getIntersectionNode(create_listnode([4, 1, 8, 4, 5]), create_listnode([5, 0, 1, 8, 4, 5])))
示例: 输入: 1->2->3->4->5 和 k = 2 输出: 4 说明: 给定的 k保证是有效的。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ from ListNodeHelper import ListNode, create_listnode class Solution: def kthToLast(self, head: ListNode, k: int) -> int: rtn_node = head for i in range(k): head = head.next while head: head = head.next rtn_node = rtn_node.next return rtn_node.val if __name__ == '__main__': s = Solution() print(s.kthToLast(create_listnode([1, 2, 3, 4, 5]), 5))
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ from ListNodeHelper import create_listnode, ListNode, listnode_to_list class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: root = ListNode(0) root.next = head pre = root while head and head.next: if head.next and head.val != head.next.val: pre = head head = head.next else: while head.next and head.val == head.next.val: head = head.next head = head.next pre.next = head return root.next if __name__ == '__main__': s = Solution() print( listnode_to_list( s.deleteDuplicates(create_listnode([1, 2, 3, 3, 4, 4, 5])))) print( listnode_to_list(s.deleteDuplicates(create_listnode([1, 1, 1, 2, 3]))))