[left <= right]
进行反转# 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 = 0
notehead = ListNode(0)
curr = notehead
while l1 != None or l2 != None:
n = l1.val if l1 != None else 0
m = l2.val if l2 != None else 0
sum = carry + n + m
carry = int(sum/10)
curr.next = ListNode(sum%10)
curr = curr.next
if l1 != None: l1 = l1.next # 进入下一个
if l2 != None: l2 = l2.next
if carry > 0:
curr.next = ListNode(carry)
return notehead.next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
res = ListNode(0, head)
fist = head
second = res
for _ in range(n):
fist = fist.next
while fist:
fist = fist.next
second = second.next
second.next = second.next.next
return res.next
class Solution:
def mergeTwoLists(self, list1, list2):
if not list1: return list2
if not list2: return list1
if list1.val < list2.val:
l3, list1 = list1, list1.next
else:
l3, list2 = list2, list2.next
cur = l3
while list1 and list2:
if list1.val < list2.val:
cur.next, list1 = list1, list1.next
else:
cur.next, list2 = list2, list2.next
cur = cur.next
cur.next = list1 if list1 else list2
return l3
class Solution:
def mergeTwoLists(self, list1, list2):
if not list1: return list2
if not list2: return list1
if list1.val < list2.val:
l3, list1 = list1, list1.next
else:
l3, list2 = list2, list2.next
cur = l3
while list1 and list2:
if list1.val < list2.val:
cur.next, list1 = list1, list1.next
else:
cur.next, list2 = list2, list2.next
cur = cur.next
cur.next = list1 if list1 else list2
return l3
def mergeKLists(self, lists):
if len(lists) == 0:
return None
if 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
class Solution:
def swapPairs(self, head):
if not head: return head
res = ListNode(0, head)
c = res
while c.next and c.next.next:
a, b = c.next, c.next.next
c.next, a.next = b, b.next
b.next = a
c = c.next.next
return res.next
class Solution:
# 翻转一个子链表,并且返回新的头与尾
def reverse(self, head: ListNode, tail: ListNode):
prev = tail.next
p = head
while prev != tail:
nex = p.next
p.next = prev
prev = p
p = nex
return tail, head
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
hair = ListNode(0)
hair.next = head
pre = hair
while head:
tail = pre
# 查看剩余部分长度是否大于等于 k
for i in range(k):
tail = tail.next
if not tail:
return hair.next
nex = tail.next
head, tail = self.reverse(head, tail)
# 把子链表重新接回原链表
pre.next = head
tail.next = nex
pre = tail
head = tail.next
return hair.next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if k == 0 or not head or not head.next:
return head
cur = head
n = 1
while cur.next:
cur = cur.next
n += 1
add = n - k % n
if add == n: # 倍数的时候
return head
cur.next = head
while add:
cur = cur.next
add -= 1
res = cur.next
cur.next = None
return res
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
cur = head
while cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
return head
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
pre = ListNode(0, head)
cur = pre
while cur.next and cur.next.next:
if cur.next.val == cur.next.next.val:
x = cur.next.val
while cur.next and cur.next.val == x:
cur.next = cur.next.next
else:
cur = cur.next
return pre.next
class Solution:
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
small = ListNode()
large = ListNode()
pre_small = small
pre_large = large
while head:
if head.val < x:
small.next = head
small = small.next
else:
large.next = head
large = large.next
head = head.next
large.next = None
small.next = pre_large.next
return pre_small.next
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
pre_head = ListNode(0, head)
pre = pre_head
for _ in range(left -1):
pre = pre.next
cur = pre.next
for _ in range(right - left):
tmp = cur.next
cur.next = cur.next.next
tmp.next = pre.next
pre.next = tmp
return pre_head.next
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 = None
pre.right = cur
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
my_dict = {}
def copynode(node):
if node is None: return None
if my_dict.get(node):
return my_dict.get(node)
root = Node(node.val) # 存入字典
my_dict[node] = root
root.next = copynode(node.next) # 继续获取下一个节点
root.random = copynode(node.random) # 继续获取下一个随机节点
return root
return copynode(head)
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next:
return False
slow = head
fast = head.next
while slow != fast:
if not fast or not fast.next:
return False
slow = slow.next
fast = fast.next.next
return True
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = head
fast = head
while fast:
slow = slow.next
if not fast.next:
return None
fast = fast.next.next
if fast == slow:
pre = head
while pre != slow:
slow = slow.next
pre = pre.next
return pre
return None
class Solution:
def reorderList(self, head):
if not head: return
ltmp = []
cur = head
while cur: # 转换成列表
ltmp.append(cur)
cur = cur.next
n = 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 # 最后一个指向空
class Solution:
def sortList(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
slow = head
fast = head
while fast.next and fast.next.next: # 用快慢指针分成两部分
slow = slow.next
fast = fast.next.next
mid = slow.next # 找到左右部分, 把左部分最后置空
slow.next = None
left = self.sortList(head) # 递归下去
right = self.sortList(mid)
return self.merge(left, right) # 合并
def merge(self, left, right):
dummy = ListNode(0)
p = dummy
l = left
r = right
while l and r:
if l.val < r.val:
p.next = l
l = l.next
p = p.next
else:
p.next = r
r = r.next
p = p.next
if l:
p.next = l
if r:
p.next = r
return dummy.next
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
if headA is None or headB is None:
return None
pa = headA
pb = headB
while pa != pb:
pa = pa.next if pa else headB
pb = pb.next if pb else headA
return pa
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head: return head
pre = None
cur = head
next = head.next
while next:
cur.next = pre
pre = cur
cur = next
next = next.next
cur.next = pre
return cur
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
vals = []
cur = head
while cur:
vals.append(cur.val)
cur = cur.next
return vals == vals[::-1]
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head is None:
return head
pre = ListNode(0)
pre.next = head
cur = pre
while cur and cur.next:
if cur.next.val == val:
cur.next = cur.next.next
cur = cur.next
return pre.next
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
res = ListNode(0)
res.next = head
fist = head
second = res
for _ in range(k):
fist = fist.next
while fist:
fist = fist.next
second = second.next
return second.next
相关参考:
声明: 总结学习,有问题或不当之处,可以批评指正哦,谢谢。