-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
123 lines (96 loc) · 3.39 KB
/
LinkedList.java
File metadata and controls
123 lines (96 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package interviews;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
public class LinkedList {
public static Node head;
public class Node {
/*creating a value node */
int value;
Node next;
/* constructor for intializing the node*/
Node(int d) {
value = d;
next = null;
}
}
public void InsertValue(int new_value)
{
/* Allocate the Node, put in the value Set next as null */
Node new_node = new Node(new_value);
/* If the Linked List is empty, then make the new node as head */
if (head == null)
{
head = new Node(new_value);
return;
}
/* This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* Change the next of last node */
last.next = new_node;
return;
}
/* Function to reverse the linked list */
Node reverse(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// prints content of double linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
}
public ArrayList<Integer> covertToArrayList(Node node) {
ArrayList<Integer> myList = new ArrayList<Integer>();
while (node != null) {
myList.add(node.value);
node = node.next;
}
return myList;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner scan = new Scanner(System.in);
try {
/* Uncomment this if you want to take input from command line*/
/*for(int i=0; i<args.length;i++)
list.InsertValue(Integer.parseInt(args[i])); */
/* taking input from console*/
System.out.println(" enter the size of the list:");
int ListSize = Integer.parseInt(scan.nextLine());
/* taking in list values */
int ListElements[] = new int[ListSize];
System.out.println("Enter the integer values to be inserted in the list:" );
for(int i=0; i<ListSize;i++){
ListElements[i] = scan.nextInt();
list.InsertValue(ListElements[i]);}
System.out.println("Given list");
list.printList(head);
head = list.reverse(head);
System.out.println("");
System.out.println("Reversed list ");
list.printList(head);
System.out.println("Time Complexity: O(n),Space Complexity: O(1)");
} catch (Exception e) {
System.out.println("invalid input, please provide proper inputs");
System.out.println("Size of the list should be a positive integer & all the elements in the list should be integers");
}
}
}