java - duplicate the singly linked list -
[3, 5, 4, 2, 1] need delete nodes close tail should [1, 2, 3, 5, 4] suggestions?
public void delete() { for(node<t> current = gethead(); current != null; current = current.getnext()){ system.out.println(temp.getvalue()); removevalue(temp.getvalue()); } } } }
you don't need remove @ (i mean not calling removevalue
). store values encounter in set , if value in set, re-link list in consequence. if don't have right utilize library code, implement set binary search tree, easy , quite efficient.
this how do, assuming have implementation of set
:
public void makeunique() { set<t> set = new myset<>(); node<t> current = gethead(); node<t> previous = null; while (current != null) { // if current.getvalue() in set, skip node // (the add together method of set returns true iff element not // in set , if not, adds set) if (set.add(current.getvalue()) { // previous represents lastly node inserted in set. // duplicate values encountered since have been ignored // previous.setnext(current) re-links list, removing duplicates if (previous != null) { previous.setnext(current); current.setprevious(previous); } previous = current; } current = current.getnext(); } }
java data-structures linked-list nodes
No comments:
Post a Comment