Everything seems to be working fine, the only problem being that I am not able to use my printLinked method to print the reversed order of integers. I am able to output the reversed list via a println in my invert() method, so therefore I seem to at least have that method working for the most part.
Can anyone please let me know what I am doing wrong? I have to print the inverted list of integers via the printLinked() method and seem to be missing something in order to make this happen.
Thanks.
import java.util.Scanner;
public class LinkedList
{
public static void main(String[] args)
{
ListNode head = new ListNode();
ListNode tail = head;
tail.link = null;
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a list of integers seperated with blanks and ending with a 0");
input = keyboard.nextInt();
ListNode temp;
while(input != 0)
{
temp = new ListNode();
temp.data = input;
temp.link = null;
tail.link = temp;
tail = temp;
input = keyboard.nextInt();
}
System.out.println("You entered the following integers : " );
printLinked(head);
delrep(head);
System.out.println("After deleting repeating integers, you are left with : " );
printLinked(head);
invert(head);
System.out.println("Your inverted list of integers is : ");
printLinked(head);
}
public static void printLinked(ListNode list)
{
ListNode cursor = list;
while (cursor != null)
{
System.out.print(cursor.data + " ");
cursor = cursor.link;
}
System.out.println("");
}
public static void delrep(ListNode head)
{
ListNode previous = head;
ListNode current = previous.link;
while (current != null)
{
ListNode dummy = head;
while (dummy != current)
{
if (dummy.data == current.data)
{
ListNode temp = current.link;
previous.link = temp;
current = temp;
}
dummy = dummy.link;
}
if (dummy == current)
{
previous = current;
current = current.link;
}
}
}
public static void invert(ListNode head)
{
ListNode temp1 = null;
ListNode temp2 = null;
while (head != null)
{
temp1 = head.link;
head.link = temp2;
temp2 = head;
head = temp1;
}
head = temp2;
while (head.link != null)
{
System.out.print(head.data + " ");
head = head.link;
}
System.out.println("");
}
}

New Topic/Question
Reply



MultiQuote



|