I want to reverse a linked list . I am able to reverse the list but in doing so, original list is also affected .How to see both original and reverse linkedlist while using display function?
Asking for the below codebase...
Please help with the display and reverseLink method . Added all the other classes as well.
public class Node {
int data;
Node link;
public Node(int data, Node link) {
this.data = data;
this.link = link;
}
public Node() {
this.data = 0;
this.link = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
}
public class Sa_LinkedList {
Node start;
Node end;
int size;
public Sa_LinkedList() {
start=null;
end=null;
size=0;
}
public boolean isEmpty() {
return start==null;
}
public int getSize() {
return size;
}
public void insertAtStart(int val) {
Node newNd=new Node(val,null);
size ;
if(start==null) {
start=newNd;
end=start;
}
else
{
newNd.setLink(start);
start=newNd;
}
}
public void insertAtEnd(int val) {
Node newNd=new Node(val,null);
size ;
if(end==null) {
end=start=newNd;
}
else {
end.setLink(newNd);
end=newNd;
}
}
public void insertAtPos(int val,int pos) {
Node newNd=new Node(val,null);
Node ptr=start;
pos=pos-1;
for(int i=0;i<size;i ) {
if(i==pos) {
Node temp=ptr.getLink();
ptr.setLink(newNd);
newNd.setLink(temp);
break;
}
ptr=ptr.getLink();
}
size ;
}
public void deleteAtPos(int pos) {
if(pos==1) {
start=start.getLink();
size--;
return;
}
if(pos==size) {
Node s=start;
Node t=start;
while(t!=end) {
s=t;
t=t.getLink();
}
end=s;
end.setLink(null);
size--;
return;
}
Node ptr=start;
pos=pos-1;
for(int i=0;i<size;i ) {
if(i==pos) {
Node temp=ptr.getLink();
temp=temp.getLink();
ptr.setLink(temp);
break;
}
ptr=ptr.getLink();
}
size--;
}
//*Function for displaying the linkedlist
public void display() {
System.out.println("Displaying the linkedlist elements:");
Node ptr=start;
if (size==0)
{
System.out.println("sorry...------List is empty------");
return;
}
if(start.getLink()==null)
{
System.out.println(start.getData());
return;
}
while(ptr.getLink()!=null) {
System.out.println(ptr.getData() ",");
ptr=ptr.getLink();
}
System.out.println(ptr.getData() "\n");
}
public void reverseLink() {
Node current=start;
Node next=null;
Node prev=null;
while(current!=null) {
next=current.getLink();
current.setLink(prev);
prev=current;
current=next;
}
start=prev;
}
}
CodePudding user response:
- Copy the list and reverse one or
- or build a Doubly linked list. In a Doubly linked list each node knows its previous and next node. With this you can easily print the list in reversed order
CodePudding user response:
There is Collections.reverse(List) method available in JDK. You could us its approach as an example:
public static void reverse(List<?> list) {
int size = list.size();
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
for (int i=0, mid=size>>1, j=size-1; i<mid; i , j--)
swap(list, i, j);
} else {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i<mid; i ) {
Object tmp = fwd.next();
fwd.set(rev.previous());
rev.set(tmp);
}
}
}
