206. 反转链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode):
# 1. 迭代
pre, cur = None, head
while cur != None:
tmp = cur.next
cur.next = pre
pre, cur = cur, tmp
return pre
def reverseList2(self, head: ListNode):
# 2. 递归
## 递归结束条件
if head == None or head.next == None:
return head
cur = self.reverseList(head.next)
head.next.next = head
head.next = None
return cur
19. 删除链表的倒数第 N 个结点
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int):
dummy = ListNode(0, head)
cur = dummy
# 获得长度
def get_lenth(head):
lenth = 0
# get_lenth
while head:
lenth += 1
head = head.next
return lenth
count, lenth = 0, get_lenth(head)
# 到倒数n+1的位置,因为我们到了倒数n的时候需要它的pre;
while count != lenth - n:
count += 1
cur = cur.next
cur.next = cur.next.next
return dummy.next
def removeNthFromEnd(self, head: ListNode, n: int):
# 快慢指针
## 快指针和慢指针之间间隔n;
## 停下的条件是快指针指向None,慢指针指向target的前一个节点;
dummy = ListNode(-1, head)
l, r = dummy, dummy
# 快指针先行!
for _ in range(n + 1):
r = r.next
# 快慢指针并行!
while r != None:
l, r = l.next, r.next
# 找到了target的前一个节点,开始删除!
l.next = l.next.next
return dummy.next
21. 合并两个有序链表
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# 递归
## 终止条件
if list1 == None:
return list2
if list2 == None:
return list1
if list1.val > list2.val:
list2.next = self.mergeTwoLists(list1, list2.next)
return list2
else:
list1.next = self.mergeTwoLists(list1.next, list2)
return list1
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# 迭代
## Special Case
if list1 == None: return list2
if list2 == None: return list1
dummy = ListNode(-1)
cur = dummy
while list1 != None and list2 != None:
if list1.val > list2.val:
cur.next = list2
list2 = list2.next # 位置不要搞错了不然下一步直接把List2搞空了;
cur = cur.next
cur.next = None
else:
cur.next = list1
list1 = list1.next # 同前!
cur = cur.next
cur.next = None
if list1 == None:
cur.next = list2
else:
cur.next = list1
return dummy.next
82. 删除排序链表中的重复元素 II
迭代写得好丑orz
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
## 迭代法
class Solution:
def deleteDuplicates(self, head: ListNode):
if not head or not head.next: return head
dummy = ListNode(-1, head)
back, cur = dummy, head
while cur and cur.next:
if cur.val != cur.next.val:
back = back.next
# 如果相等的话 要看看有几个不等
else:
while cur.next and back.next.val == cur.next.val:
cur = cur.next
back.next = cur.next
cur = cur.next
return dummy.next
## 递归法
class Solution:
def deleteDuplicates(self, head: ListNode):
if not head or not head.next: return head
# 试试递归吧
if head.val == head.next.val:
tmp = head.next
while tmp and tmp.val == head.val:
tmp = tmp.next
# 相当于把重复得节点删了233
return self.deleteDuplicates(tmp)
else:
# 不等的话就是下一个节点gogogo
head.next = self.deleteDuplicates(head.next)
return head
86. 分割链表
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
# 维护两个链表再拼接
# 这里是再创建了Node
small = ListNode(0)
large = ListNode(0)
curs, curl = small, large
cur = head
while cur:
if cur.val < x:
curs.next = ListNode(cur.val)
curs = curs.next
else:
curl.next = ListNode(cur.val)
curl = curl.next
cur = cur.next
curs.next = large.next
return small.next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
# 直接用原始的Node
small = ListNode(0)
large = ListNode(0)
curs, curl = small, large
cur = head
while cur:
tmp = cur.next
if cur.val < x:
curs.next = cur
cur.next = None
curs = curs.next
else:
curl.next = cur
cur.next = None
curl = curl.next
cur = tmp
curs.next = large.next
return small.next
92. 反转链表 II
最最最直观的想法就是把要反转的部分抽出来,反转完了之后再装回去;剩下的就是怎么设指针,设多少的问题了。但是这玩意时间复杂度咋那么高呢orz…
遇事不决设dummy2333
and 感觉206. 反转链表要回炉重造了;递归杀我!递归杀我啊!
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int):
if left == right:
return head
dummy = ListNode(-1, head)
l, r = dummy, dummy
# 先到达目的地
while right > 0:
r = r.next
right -= 1
while left > 1:
l = l.next
left -= 1
# 记录一下要用的节点的位置
subl = l.next
res = r.next
# 要反转的为[subl, r]
# 先断开
l.next = None
r.next = None
# 然后开始翻转
# 再找个指针标记一下
tmp = r
def reverse(head):
if not head or not head.next:
return head
cur = reverse(head.next)
head.next.next = head
head.next = None
return cur
tmp2 = reverse(subl)
l.next = r
subl.next = res
return dummy.next