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 remove duplicates from an ordered linked list by C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article introduces in detail "how C++ removes duplicates in ordered linked lists". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how C++ removes duplicates in ordered linked lists" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Remove Duplicates from Sorted List removes duplicates from ordered linked lists

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1-> 1-> 2

Output: 1-> 2

Example 2:

Input: 1-> 1-> 2-> 3-> 3

Output: 1-> 2-> 3

This problem allows us to remove duplicates from a given ordered linked list, so we can traverse the linked list, and each node is compared with the following node. If the node value is the same, just skip the next pointer of the previous node over the next node of the same value and point to the next node. If you traverse in this way, all duplicate nodes will be skipped, and the linked list will have no duplicates. The code is as follows:

Solution 1:

Class Solution {public: ListNode* deleteDuplicates (ListNode* head) {ListNode* cur = head; while (cur & & cur- > next) {if (cur- > val = = cur- > next- > val) {cur- > next = cur- > next- > next;} else {cur = cur- > next;}} return head;}}

We can also use a recursive method to do this, first determine whether there are at least two nodes, and if not, return head directly. Otherwise, a recursive function is called on head- > next and assigned to head- > next. Here may be a bit dizzy, first look at the following sentence, when returning, the head node is compared with the node behind it. If the value is the same, then the latter node is returned, and the current head node is skipped, and if different, the head node is returned. It can be found that the substantive deletion operation is carried out in the last sentence. Let's take a look at the second sentence. If the recursive function is called on the node after head, then there will be no duplicates in the linked list that should be returned by suppose. At this time, after receiving the head node, check whether the head is duplicate again in the third sentence. In fact, the recursion goes all the way to the end node, and then goes back again and again to delete the duplicate node. See the code as follows:

Solution 2:

Class Solution {public: ListNode* deleteDuplicates (ListNode* head) {if (! head | |! head- > next) return head; head- > next = deleteDuplicates (head- > next); return (head- > val = = head- > next- > val)? Head- > next: head;}}; after reading this, the article "how to remove duplicates from ordered linked lists by C++" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

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