Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete duplicate elements with python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to delete duplicate elements with python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to delete duplicate elements with python" together!

For a given ordered list, delete duplicate elements, leaving only elements that appear once.

Idea: When an element is compared to the next element, if they are the same, the next pointer of the current element points to the next pointer of the next element.

Language : c

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* deleteDuplicates(struct ListNode* head) { struct ListNode* cur = (int *)malloc(sizeof(struct ListNode)); cur = head;while(cur != NULL){while(cur->next != NULL && cur->val == cur->next->val){ cur->next = cur->next->next; } cur = cur->next; }return head;}

Language : cpp

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode* cur = head;while(cur != NULL){while(cur->next != NULL && cur->val == cur->next->val){ cur->next = cur->next->next; } cur = cur->next; } return head; }};

Language : python

# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object):def deleteDuplicates(self, head):""" :type head: ListNode :rtype: ListNode """ now = headwhile head:while head.next and head.val == head.next.val: head.next = head.next.next head = head.nextreturn now thank you for reading, the above is "how to use python to delete duplicate elements" content, after the study of this article, I believe everyone on how to use python to delete duplicate elements this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report