力扣的删除排序链表重复元素怎么改写成长尾词?

2026-04-19 12:161阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计632个文字,预计阅读时间需要3分钟。

力扣的删除排序链表重复元素怎么改写成长尾词?

给定一个已排序的链表的头节点 `head`,删除所有重复的元素,使得每个元素只出现一次。返回修改后的排序链表。

pythonclass ListNode: def __init__(self, val=0, next=None): self.val=val self.next=next

def delete_duplicates(head): if not head: return head

current=head while current.next: if current.val==current.next.val: current.next=current.next.next else: current=current.next return head

辅助函数,用于创建链表def create_linked_list(arr): if not arr: return None head=ListNode(arr[0]) current=head for val in arr[1:]: current.next=ListNode(val) current=current.next return head

辅助函数,用于打印链表def print_linked_list(head): current=head while current: print(current.val, end= ) current=current.next print()

示例 1head1=create_linked_list([1, 1, 2])print_linked_list(delete_duplicates(head1)) # 输出:1 2

示例 2head2=create_linked_list([1, 1, 2, 3, 3])print_linked_list(delete_duplicates(head2)) # 输出:1 2 3

给定一个已排序的链表的头head删除所有重复的元素,使每个元素只出现一次。返回已排序的链表

示例 1:

输入:head = [1,1,2] 输出:[1,2]

示例 2:

输入:head = [1,1,2,3,3] 输出:[1,2,3]

提示:

力扣的删除排序链表重复元素怎么改写成长尾词?

  • 链表中节点数目在范围[0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序排列

/** * Definition for singly-linked list. * 单链表的定义 * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { // 检查链表是否为空或只包含一个元素 // 如果是,没有重复元素需要删除,直接返回原链表 if (head == null || head.next == null) return head; // 检查当前节点和下一个节点的值是否相等 // 如果相等,跳过当前节点,对下一个节点进行递归调用deleteDuplicates // 这样可以移除所有连续的重复节点 if (head.val == head.next.val) { head = deleteDuplicates(head.next); } else { // 如果当前节点和下一个节点的值不相等, // 将下一个节点设置为deleteDuplicates递归调用后的结果 head.next = deleteDuplicates(head.next); } return head; } }

本文共计632个文字,预计阅读时间需要3分钟。

力扣的删除排序链表重复元素怎么改写成长尾词?

给定一个已排序的链表的头节点 `head`,删除所有重复的元素,使得每个元素只出现一次。返回修改后的排序链表。

pythonclass ListNode: def __init__(self, val=0, next=None): self.val=val self.next=next

def delete_duplicates(head): if not head: return head

current=head while current.next: if current.val==current.next.val: current.next=current.next.next else: current=current.next return head

辅助函数,用于创建链表def create_linked_list(arr): if not arr: return None head=ListNode(arr[0]) current=head for val in arr[1:]: current.next=ListNode(val) current=current.next return head

辅助函数,用于打印链表def print_linked_list(head): current=head while current: print(current.val, end= ) current=current.next print()

示例 1head1=create_linked_list([1, 1, 2])print_linked_list(delete_duplicates(head1)) # 输出:1 2

示例 2head2=create_linked_list([1, 1, 2, 3, 3])print_linked_list(delete_duplicates(head2)) # 输出:1 2 3

给定一个已排序的链表的头head删除所有重复的元素,使每个元素只出现一次。返回已排序的链表

示例 1:

输入:head = [1,1,2] 输出:[1,2]

示例 2:

输入:head = [1,1,2,3,3] 输出:[1,2,3]

提示:

力扣的删除排序链表重复元素怎么改写成长尾词?

  • 链表中节点数目在范围[0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序排列

/** * Definition for singly-linked list. * 单链表的定义 * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { // 检查链表是否为空或只包含一个元素 // 如果是,没有重复元素需要删除,直接返回原链表 if (head == null || head.next == null) return head; // 检查当前节点和下一个节点的值是否相等 // 如果相等,跳过当前节点,对下一个节点进行递归调用deleteDuplicates // 这样可以移除所有连续的重复节点 if (head.val == head.next.val) { head = deleteDuplicates(head.next); } else { // 如果当前节点和下一个节点的值不相等, // 将下一个节点设置为deleteDuplicates递归调用后的结果 head.next = deleteDuplicates(head.next); } return head; } }