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/01 Report--
This article introduces the relevant knowledge of "what is the algorithm for deleting redundant nodes with the same value of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
This is an algorithm problem, write algorithm problem most hate no diagram, understand people do not need to read your article, do not understand you no matter how to explain there are few diagrams to be easy to understand, let's analyze this problem.
I don't have a better solution yet, although there is a solution, but the time complexity is a little high, let's take a look at my thinking first.
Insert a picture description here
This is an unordered single linked list, we use the most stupid method, first point to the first node, its element value is 2, and then traverse all the nodes after the node, if any node element value is the same, then delete; after all traversal, we point to the second node, and then do the same operation.
Look at the illustration:
Insert a picture description here
There are two pointer variables p and Q, both of which point to the first node of the single linked list. Instead of moving the pointer p, we let the pointer Q traverse all the nodes after it.
Insert a picture description here
First, let the node pointed by the pointer p be compared with the latter node. Here, for convenience of operation, let's not move the pointer Q for a moment, but compare it like this: P-> data = = Q-> next-> data;. If it is not equal, let Q point to the next node: P = p-> next. If equal, you should first save the next node: r = Q-> next, then let the Q pointer point to the next node of the next node: Q = r-> next, and release the node memory that r points to.
In this way, a node duplicated with the header node is successfully deleted, and then the comparison continues in the same way until the entire single linked list is traversed, and there are no duplicate nodes in the single linked list. Then we have to change the point of the p pointer to the next node of the first node, and then let Q point to the next node, continue traversing, and delete all nodes in the single linked list that repeat with the second node. And so on, until the pointer p also traverses the entire single-linked list, the algorithm ends.
We have just deleted a node, so then p should point to the next node:
Insert a picture description here
At this point, let the pointer p point to the node and the element value of the next node is not equal, then let Q point directly to the next node: Q = Q-> next.
Insert a picture description here
Continue to compare the element value of the next node pointed to by Q with the element value of the node pointed to by p, and find that it is not equal. Continue to move Q at this time, and the pointer field of Q after moving is NULL, indicating that the traversal is over, and the pointer p should be moved at this time.
Insert a picture description here
By comparison, it is found that the element value of the next node is equal to it, and then delete the next node:
Insert a picture description here
At this time, the pointer domain of p is also NULL, and the algorithm ends.
The code is as follows:
LinkList DeleteRepeat (LinkList l) {
LinkList p,q,r
P = l-> next
While (p! = NULL) {
Q = p
While (Q-> next! = NULL) {
If (p-> data = = Q-> next- > data) {
R = Q-> next
Q = r-> next
Free (r)
} else {
Q = Q-> next
}
}
P = p-> next
}
Return l
} this is the end of the content of "what is the algorithm for deleting redundant nodes with the same value by Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.