In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to delete the repeated elements in the sorted list in LeetCode. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Description of the topic
Given a sorted linked list, delete all duplicate elements so that each element appears only once.
Example 1:
Input: 1-> 1-> 2 output: 1-> 2
Example 2:
Input: 1-> 1-> 2-> 3-> 3 output: 1-> 2-> 3.
This question examines the basic pointer operation of the linked list, as long as you find the node pointer to be deleted while traversing the linked list, and then delete it, as follows:
Iterate through the copy of the header node current of the linked list until each traversal of the last node determines whether the current node value is equal to the next node value. If so, delete the next node. If not, continue traversing the next node and finally return to the chain header node class Solution {
Public:
ListNode* deleteDuplicates (ListNode* head) {
/ / 0. Empty linked list returns nullptr
If (head = = nullptr)
Return nullptr
/ / 1. No head pointer operation, the head pointer is used to return the result
ListNode * cur = head
ListNode * del_node = nullptr
While (current- > next! = nullptr) {
/ / 2. Find a node with the same element
If (cur- > val = = cur- > next- > val) {
/ / 3. Save the node to be deleted
Del_node = cur- > next
/ / 4. Disconnect del_node nod
Cur- > next = del_node- > next
/ / 5. Delete nod
Delete del_node
/ / 6. Programming specification: prevent the occurrence of wild pointers
Del_node = nullptr
} else {
/ / 7. Continue to traverse backwards without finding the same element
Cur = cur- > next
}
}
Return head
}
}
Complexity analysis time complexity: O (n), only need to traverse n linked list nodes at a time space complexity: O (1), only use constant memory pointer units
The above is how to delete the repeated elements in the sorted list in LeetCode. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.