问题描述:
- LeetCode: 2. 两数相加 , 注意是逆序存储,相加求和
- LeetCode: 19. 删除链表的倒数第 N 个结点
- LeetCode: 21. 合并两个有序链表
- LeetCode: 23. 合并 K 个升序链表
- LeetCode: 24. 两两交换链表中的节点 ,两两相邻的节点交换
- LeetCode: 25. K 个一组翻转链表
- LeetCode: 61. 旋转链表
- LeetCode: 83. 删除排序链表中的重复元素 ,已经排序,只保留一个
- LeetCode: 82. 删除排序链表中的重复元素 II ,已经排序,一个不保留
- LeetCode: 86. 分隔链表 ,根据一个元素判断,小的在前面大的在后面
- LeetCode: 92. 反转链表 II ,给定一个区间
[left <= right]
进行反转 - LeetCode: 114. 二叉树展开为链表 ,先序遍历类型
- LeetCode: 138. 复制带随机指针的链表
- LeetCode: 141. 环形链表 ,判断是否存在环
- LeetCode: 142. 环形链表 II ,找环的入口
- LeetCode: 143. 重排链表 ,首尾交替链接类型
- LeetCode: 148. 排序链表 ,由小到大排序,归并排序即可
- LeetCode: 160. 相交链表 ,求两个链表的交点
- LeetCode: 206. 反转链表 ,头插发
- LeetCode: 234. 回文链表 ,输出反转
- 剑指 Offer 18. 删除链表的节点 ,正常删除类型
- 剑指 Offer 22. 链表中倒数第k个节点 ,查找类型
(1)LeetCode: 2. 两数相加 Python3实现:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:carry = 0notehead = ListNode(0)curr = noteheadwhile l1 != None or l2 != None:n = l1.val if l1 != None else 0m = l2.val if l2 != None else 0sum = carry + n + mcarry = int(sum/10)curr.next = ListNode(sum%10)curr = curr.nextif l1 != None: l1 = l1.next # 进入下一个if l2 != None: l2 = l2.next if carry > 0:curr.next = ListNode(carry)return notehead.next
(2)LeetCode: 19. 删除链表的倒数第 N 个结点 Python3实现:
class Solution:def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:res = ListNode(0, head)fist = headsecond = resfor _ in range(n):fist = fist.nextwhile fist:fist = fist.nextsecond = second.nextsecond.next = second.next.nextreturn res.next
(3)LeetCode: 21. 合并两个有序链表 Python3实现:
class Solution:def mergeTwoLists(self, list1, list2):if not list1: return list2if not list2: return list1if list1.val < list2.val:l3, list1 = list1, list1.nextelse:l3, list2 = list2, list2.nextcur = l3while list1 and list2:if list1.val < list2.val:cur.next, list1 = list1, list1.nextelse:cur.next, list2 = list2, list2.nextcur = cur.nextcur.next = list1 if list1 else list2return l3
(4)LeetCode: 23. 合并 K 个升序链表 Python3实现:
class Solution:def mergeTwoLists(self, list1, list2):if not list1: return list2if not list2: return list1if list1.val < list2.val: l3, list1 = list1, list1.nextelse:l3, list2 = list2, list2.nextcur = l3while list1 and list2:if list1.val < list2.val: cur.next, list1 = list1, list1.nextelse: cur.next, list2 = list2, list2.nextcur = cur.nextcur.next = list1 if list1 else list2return l3def mergeKLists(self, lists):if len(lists) == 0:return Noneif len(lists) == 1:return lists[0]k = len(lists)res = lists[0]for i in range(1, k):res = self.mergeTwoLists(res, lists[i])return res
(5)LeetCode: 24. 两两交换链表中的节点 Python3实现:
class Solution:def swapPairs(self, head):if not head: return headres = ListNode(0, head)c = reswhile c.next and c.next.next:a, b = c.next, c.next.nextc.next, a.next = b, b.nextb.next = ac = c.next.nextreturn res.next
(6)LeetCode: 25. K 个一组翻转链表 Python3实现:
class Solution:# 翻转一个子链表,并且返回新的头与尾def reverse(self, head: ListNode, tail: ListNode):prev = tail.nextp = headwhile prev != tail:nex = p.nextp.next = prevprev = pp = nexreturn tail, headdef reverseKGroup(self, head: ListNode, k: int) -> ListNode:hair = ListNode(0)hair.next = headpre = hairwhile head:tail = pre# 查看剩余部分长度是否大于等于 kfor i in range(k):tail = tail.nextif not tail:return hair.nextnex = tail.nexthead, tail = self.reverse(head, tail)# 把子链表重新接回原链表pre.next = headtail.next = nexpre = tailhead = tail.nextreturn hair.next
(7)LeetCode: 61. 旋转链表 Python3实现:
class Solution:def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:if k == 0 or not head or not head.next:return headcur = headn = 1while cur.next:cur = cur.nextn += 1add = n - k % n if add == n: # 倍数的时候return headcur.next = headwhile add:cur = cur.nextadd -= 1res = cur.nextcur.next = Nonereturn res
(8)LeetCode: 83. 删除排序链表中的重复元素 Python3实现:
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:if not head:return headcur = headwhile cur.next:if cur.next.val == cur.val:cur.next = cur.next.nextelse:cur = cur.nextreturn head
(9)LeetCode: 82. 删除排序链表中的重复元素 II Python3实现:
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:if not head:return headpre = ListNode(0, head)cur = prewhile cur.next and cur.next.next:if cur.next.val == cur.next.next.val:x = cur.next.valwhile cur.next and cur.next.val == x:cur.next = cur.next.nextelse:cur = cur.nextreturn pre.next
(10)LeetCode: 86. 分隔链表 Python3实现:
class Solution:def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:small = ListNode()large = ListNode()pre_small = smallpre_large = largewhile head:if head.val < x:small.next = headsmall = small.nextelse:large.next = headlarge = large.nexthead = head.nextlarge.next = Nonesmall.next = pre_large.nextreturn pre_small.next
(11)LeetCode: 92. 反转链表 II Python3实现:
class Solution:def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:pre_head = ListNode(0, head)pre = pre_headfor _ in range(left -1):pre = pre.nextcur = pre.nextfor _ in range(right - left):tmp = cur.nextcur.next = cur.next.nexttmp.next = pre.nextpre.next = tmpreturn pre_head.next
(12)LeetCode: 114. 二叉树展开为链表 Python3实现:
class Solution:def flatten(self, root: Optional[TreeNode]) -> None:"""Do not return anything, modify root in-place instead."""res = []def dfs(root):if root:res.append(root)if root.left: dfs(root.left)if root.right: dfs(root.right)dfs(root)n = len(res)for i in range(1, n):pre, cur = res[i-1], res[i]pre.left = Nonepre.right = cur
(13)LeetCode: 138. 复制带随机指针的链表 Python3实现:
class Solution:def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':my_dict = {}def copynode(node):if node is None: return Noneif my_dict.get(node): return my_dict.get(node)root = Node(node.val) # 存入字典my_dict[node] = rootroot.next = copynode(node.next) # 继续获取下一个节点root.random = copynode(node.random) # 继续获取下一个随机节点return rootreturn copynode(head)
(14)LeetCode: 141. 环形链表 Python3实现:
class Solution:def hasCycle(self, head: ListNode) -> bool:if not head or not head.next:return Falseslow = headfast = head.nextwhile slow != fast:if not fast or not fast.next:return Falseslow = slow.nextfast = fast.next.nextreturn True
(15)LeetCode: 142. 环形链表 II Python3实现:
class Solution:def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:slow = headfast = headwhile fast:slow = slow.nextif not fast.next:return Nonefast = fast.next.nextif fast == slow:pre = headwhile pre != slow:slow = slow.nextpre = pre.nextreturn prereturn None
(16)LeetCode: 143. 重排链表 Python3实现:
class Solution:def reorderList(self, head):if not head: returnltmp = []cur = headwhile cur: # 转换成列表ltmp.append(cur)cur = cur.nextn = len(ltmp) # 链表长度for i in range(n // 2): # 处理ltmp[i].next = ltmp[n - 1 - i]ltmp[n - 1 - i].next = ltmp[i + 1]ltmp[n // 2].next = None # 最后一个指向空
(17)LeetCode: 148. 排序链表 Python3实现:
class Solution:def sortList(self, head: ListNode) -> ListNode:if not head or not head.next:return headslow = headfast = headwhile fast.next and fast.next.next: # 用快慢指针分成两部分slow = slow.nextfast = fast.next.nextmid = slow.next # 找到左右部分, 把左部分最后置空slow.next = Noneleft = self.sortList(head) # 递归下去right = self.sortList(mid)return self.merge(left, right) # 合并def merge(self, left, right):dummy = ListNode(0)p = dummyl = leftr = rightwhile l and r:if l.val < r.val:p.next = ll = l.nextp = p.nextelse:p.next = rr = r.nextp = p.nextif l:p.next = lif r:p.next = rreturn dummy.next
(18)LeetCode: 160. 相交链表 Python3实现:
class Solution:def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:if headA is None or headB is None:return Nonepa = headApb = headBwhile pa != pb:pa = pa.next if pa else headBpb = pb.next if pb else headAreturn pa
(19)LeetCode: 206. 反转链表 Python3实现:
class Solution:def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:if not head: return headpre = Nonecur = headnext = head.nextwhile next:cur.next = prepre = curcur = nextnext = next.nextcur.next = prereturn cur
(20)LeetCode: 234. 回文链表 Python3实现:
class Solution:def isPalindrome(self, head: Optional[ListNode]) -> bool:vals = []cur = headwhile cur:vals.append(cur.val)cur = cur.nextreturn vals == vals[::-1]
(21)剑指 Offer 18. 删除链表的节点 Python3实现:
class Solution:def deleteNode(self, head: ListNode, val: int) -> ListNode:if head is None:return headpre = ListNode(0)pre.next = headcur = prewhile cur and cur.next:if cur.next.val == val:cur.next = cur.next.nextcur = cur.nextreturn pre.next
(22)剑指 Offer 22. 链表中倒数第k个节点 Python3实现:
class Solution:def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:res = ListNode(0)res.next = headfist = headsecond = resfor _ in range(k):fist = fist.nextwhile fist:fist = fist.nextsecond = second.nextreturn second.next
相关参考:
声明: 总结学习,有问题或不当之处,可以批评指正哦,谢谢。