def reverseKGroup(self, head: ListNode, k: int) -> ListNode: dummy = ListNode(0) dummy.next = head pre, end = dummy, dummy while end.next: # 取出待翻转的部分 i = 0 while i < k and end: end = end.next i += 1 if not end: break # 断开链表 startNode = pre.next nextNode = end.next end.next = None # 处理翻转 pre.next = self.reverse(startNode) # startNode 转到翻转这部分节点的最后了 # 连接断开的链表 startNode.next = nextNode # 挪动以进行下一组处理 pre = startNode end = pre return dummy.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val <= l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2
def sortList(self, head: ListNode) -> ListNode: res_ls = [] while head: res_ls.append(head.val) head = head.next if len(res_ls) == 0: return res_ls.sort() node = ListNode(res_ls[0]) new_head = node for i in res_ls[1:]: node.next = ListNode(i) node = node.next return new_head
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: dummy = ListNode(0) p = dummy while True: count = k stack = [] tmp = head while count and tmp: stack.append(tmp) tmp = tmp.next count -= 1 # 注意,目前tmp所在k+1位置 # 说明剩下的链表不够k个,跳出循环 if count: p.next = head break # 翻转操作 while stack: p.next = stack.pop() p = p.next # 与剩下链表连接起来 p.next = tmp head = tmp return dummy.next
def reverseListNode(self, head: ListNode) -> ListNode: if head.next is None: return head last = self.reverseListNode(head.next) head.next.next = head head.next = None return last
def swapPairs(self, head: ListNode) -> ListNode: if not head or not head.next: return head newHead = head.next head.next = self.swapPairs(newHead.next) newHead.next = head return newHead
def reverseList(self, head: ListNode) -> ListNode: if head.next is None: return head last = self.reverseList(head.next) # 环形链表这里vscode没法展示 head.next.next = head head.next = None return last
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: def linked_list_value(l1: ListNode) -> int: l1_value = [] while l1 is not None: l1_value.append(str(l1.val)) l1 = l1.next return int("".join(l1_value)[::-1]) l1_value = linked_list_value(l1) # print(l1_value) l2_value = linked_list_value(l2) # print(l2_value) value_ = str(l1_value + l2_value)[::-1] dummy_head = ListNode(0) t = dummy_head for i in value_: curr = ListNode(val=int(i)) t.next = curr t = t.next return dummy_head.next
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def reverseList(self, head: ListNode) -> ListNode: if head.next is None: return head last = self.reverseList(head.next) # 环形链表这里vscode没法展示 head.next.next = head head.next = None return last if __name__ == '__main__': nums = [1, 2, 3, 4, 5] ln = ListNode.list2node(nums) sol = Solution() a = sol.reverseList(ln) print(a)
from base.linked_list.ListNode import ListNode class Solution: def kthToLast(self, head: ListNode, k: int) -> int: # 快慢指针的解法 advance, last = head, head a = 0 while a < k: advance = advance.next a += 1 while advance: advance = advance.next last = last.next return last.val if __name__ == '__main__': ls = ListNode.list2node([1, 2, 3, 4, 5]) sol = Solution() print(sol.kthToLast(ls, 2))
from base.linked_list.ListNode import ListNode class Solution(object): def deleteDuplicates(self, head): if not head or not head.next: return head if head.val != head.next.val: head.next = self.deleteDuplicates(head.next) else: move = head.next while move and head.val == move.val: move = move.next return self.deleteDuplicates(move) return head if __name__ == '__main__': head1 = [1, 2, 3, 3, 4, 4, 5] head1 = ListNode.list2node(head1) head2 = [1, 1, 1, 2, 3] head2 = ListNode.list2node(head2) sol = Solution() print(sol.deleteDuplicates(head2)) print(sol.deleteDuplicates(head1))
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: reverse_headA = self.reverseListNode(headA) reverse_headB = self.reverseListNode(headB) while reverse_headA.val == reverse_headB.val: reverse_headA = reverse_headA.next reverse_headB = reverse_headB.next return self.reverseListNode(reverse_headA) def reverseListNode(self, head: ListNode) -> ListNode: if head.next is None: return head last = self.reverseListNode(head.next) head.next.next = head head.next = None return last if __name__ == '__main__': a = [1, 2, 3, 4, 5, 6] ln = ListNode.list2node(a)
from typing import List from base.linked_list.ListNode import ListNode class Solution: def reversePrint(self, head: ListNode): res = [] while head is not None: res.append(head.val) head = head.next return res[::-1] if __name__ == '__main__': head = ListNode.list2node([1, 3, 2]) sol = Solution() print(sol.reversePrint(head))
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def sortList(self, head: ListNode) -> ListNode: res_ls = [] while head: res_ls.append(head.val) head = head.next if len(res_ls) == 0: return res_ls.sort() node = ListNode(res_ls[0]) new_head = node for i in res_ls[1:]: node.next = ListNode(i) node = node.next return new_head if __name__ == '__main__': sol = Solution() a = ListNode.list2node([4, 2, 1, 3]) b = sol.sortList(a) print(b)
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val <= l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2 if __name__ == '__main__': a = [1, 2, 4] lna = ListNode.list2node(a) print(lna) b = [1, 3, 4] lnb = ListNode.list2node(b) print(lnb) sol = Solution() print(sol.mergeTwoLists(lna, lnb))
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: cur = head while cur: while cur.next and cur.next.val == cur.val: cur.next = cur.next.next cur = cur.next return head if __name__ == '__main__': ln = ListNode.list2node([1, 1, 2]) ln2 = ListNode.list2node([1, 1, 2, 3, 3]) sol = Solution() print(sol.deleteDuplicates(ln)) print(sol.deleteDuplicates(ln2))
class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: def linked_list_value(l1: ListNode) -> int: l1_value = [] while l1 is not None: l1_value.append(str(l1.val)) l1 = l1.next return int("".join(l1_value)[::-1]) l1_value = linked_list_value(l1) # print(l1_value) l2_value = linked_list_value(l2) # print(l2_value) value_ = str(l1_value + l2_value)[::-1] dummy_head = ListNode(0) t = dummy_head for i in value_: curr = ListNode(val=int(i)) t.next = curr t = t.next return dummy_head.next if __name__ == '__main__': l1 = ListNode.list2node([2, 4, 3]) l2 = ListNode.list2node([5, 6, 4]) sol = Solution() temp = (sol.addTwoNumbers(l1, l2)) print(temp)
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def removeDuplicateNodes(self, head: ListNode) -> ListNode: if not head: return head occurred = {head.val} pos = head # 枚举前驱节点 while pos.next: # 当前待删除节点 cur = pos.next if cur.val not in occurred: occurred.add(cur.val) pos = pos.next else: pos.next = pos.next.next return head if __name__ == '__main__': sol = Solution() head = ListNode.list2node([1, 2, 3, 3, 2, 1]) head2 = ListNode.list2node([1, 2, 3]) print(head) print(head2) print(sol.removeDuplicateNodes(head))
# Definition for singly-linked list. from base.linked_list.ListNode import ListNode class Solution: def getDecimalValue(self, head: ListNode) -> int: res = [] while head: res.append(str(head.val)) head = head.next return int("".join(res), base=2) if __name__ == '__main__': head = ListNode.list2node([1, 0, 1]) sol = Solution() print(sol.getDecimalValue(head))
from base.linked_list.ListNode import ListNode class Solution: def rotateRight(self, head: ListNode, k: int) -> ListNode: pass if __name__ == '__main__': sol = Solution() head = [1, 2, 3, 4, 5] head = ListNode.list2node(head) k = 2 print(sol.rotateRight(head, k))