删除有序链表的重复元素
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
newhead=ListNode()
newhead.next=head
l=newhead.next
while l:
r=l.next
pre=l
while r:
if r.val==l.val:
pre.next=r.next
r=r.next
else:
break
l=l.next
return newhead.next
删除无序链表的重复元素
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
newhead=ListNode()
newhead.next=head
l=newhead.next
while l:
r=l.next
pre=l
while r:
if r.val==l.val:
pre.next=r.next
r=r.next
else:
pre=r
r=r.next
l=l.next
return newhead.next
删除无序链表的重复元素以及产生重复的元素
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
newhead=ListNode()
newhead.next=head
l=newhead.next
pre_l=newhead
while l:
r=l.next
pre_r=l
flag=0
while r:
if l.val==r.val:
r=r.next
pre_r.next=r
flag=1
else:
pre_r=pre_r.next
r=r.next
if flag==1:
pre_l.next=l.next
else:
pre_l=l
l=l.next
return newhead.next
旋转链表II
反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:
输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
我的解答
# 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) -> ListNode:
newhead=ListNode()
newhead.next=head
l=newhead.next
l_pre=newhead
p_arr=[]
c=0
while l and c<left:
c+=1
if c==left:
break
l_pre=l
l=l.next
c=0
cl=right-left+1
r=l
while r and c<cl:
c+=1
p_arr.append(r)
if c==cl:
break
#r_pre=r
r=r.next
r_behind=r.next
for i in range(cl-1,-1,-1):
p=p_arr[i]
l_pre.next=p
l_pre=l_pre.next
l_pre.next=r_behind
return newhead.next
官方答案-穿针引线
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
def reverse_linked_list(head: ListNode):
# 也可以使用递归反转一个链表
pre = None
cur = head
while cur:
next = cur.next
cur.next = pre
pre = cur
cur = next
# 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
dummy_node = ListNode(-1)
dummy_node.next = head
pre = dummy_node
# 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
# 建议写在 for 循环里,语义清晰
for _ in range(left - 1):
pre = pre.next
# 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
right_node = pre
for _ in range(right - left + 1):
right_node = right_node.next
# 第 3 步:切断出一个子链表(截取链表)
left_node = pre.next
curr = right_node.next
# 注意:切断链接
pre.next = None
right_node.next = None
# 第 4 步:同第 206 题,反转链表的子区间
reverse_linked_list(left_node)
# 第 5 步:接回到原来的链表中
pre.next = right_node
left_node.next = curr
return dummy_node.next