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 nodes in the linked list by C++

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to delete duplicate nodes in the linked list by C++". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how C++ deletes duplicate nodes in the linked list.

Algorithm:

The core point is how to find the duplicate node. If the list is ordered, as long as the next node is a duplicate node with the same value as the current node, you can directly point the current node to the next node.

Topic 1: delete duplicate elements in the sorted list

Code implementation:

/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func deleteDuplicates (head * ListNode) * ListNode {curr: = head for curr! = nil & & curr.Next! = nil {if curr.Val = = curr.Next.Val {/ / Delete duplicate node, node tmp: = curr.Next curr.Next = tmp.Next} else {/ / if it is not a duplicate node Mobile node curr = curr.Next}} return head}

Topic 2: delete duplicate elements in the sorted list

Code implementation

/ / algorithm: different from topic 1, this topic is to delete all duplicate nodes, / / that is not only to find the location of the duplicate node, / / to record the previous node of the first duplicate node, which is called pre//, and then use pre.Next to point to which non-repeating node behind the duplicate node to complete the deletion. / / when it comes to the preorder node pre, you need to consider the Sentinel node. / * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func deleteDuplicates (head * ListNode) * ListNode {if head = = nil | | head.Next = = nil {return head} n: = new (ListNode) / / set hook node Avoid head nodes being deleted n.Next = head pre: = n for pre! = nil {/ / pre is the previous node of the current node if pre.Next = = nil {break}

Curr: = pre.Next next: = curr.Next for next! = nil {/ / compare duplicate nodes if next.Val! = curr.Val {break} next = next.Next}

If curr.Next = = next {pre = pre.Next} else {/ / repeating, deleting repeating nodes pre.Next = next}} return n.Next} so far, I believe you have a deeper understanding of "how C++ deletes duplicate nodes in the linked list". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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