-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlinkedlist.java
More file actions
171 lines (147 loc) · 4.29 KB
/
linkedlist.java
File metadata and controls
171 lines (147 loc) · 4.29 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class LinkedList<T> {
Node<T> head;
static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}
public void addFirst(T data) {
Node<T> temp = new Node<T>(data);
if (head == null) {
head = temp;
return;
}
temp.next = head;
head = temp;
}
public void addLast(T data) {
Node<T> temp = new Node<T>(data);
if (head == null) {
head = temp;
return;
}
Node<T> curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = temp;
}
public void printList() {
if(head == null){
System.out.println("List is empty");
return;
}
Node<T> curr = head;
while (curr != null) {
System.out.print(curr.data + "->");
curr = curr.next;
}
System.out.println("null");
}
public void removeFirst() {
if (head == null) {
System.out.println("List is empty. Cannot remove.");
return;
}
head = head.next;
}
public void removeLast() {
if (head == null) {
System.out.println("List is empty. Cannot remove.");
return;
}
if (head.next == null) {
// When the head is the only element
head = null;
return;
}
Node<T> curr = head;
Node<T> prev = null;
while (curr.next != null) {
prev = curr;
curr = curr.next;
}
prev.next = null;
}
public Node<T> removeLastNode(Node<T> head)
{
if (head == null)
return null;
if (head.next == null) { return null;
}
Node<T> second_last = head;
while (second_last.next.next != null)
second_last = second_last.next;
second_last.next = null;
return head;
}
/**
* This method reverses a singly linked list in-place.
* It iterates through the linked list, changing the direction
* of each node's 'next' pointer to reverse the list.
*
* Time Complexity: O(n) - Linear time, where 'n' is the number of nodes in the linked list.
* The method iterates through all 'n' nodes once.
*
* Space Complexity: O(1) - Constant space, as the method uses a fixed number of pointers (prev, current, next)
* and does not create additional data structures proportional to the input size.
*
* @param node The head of the linked list to be reversed.
* @return The new head of the reversed linked list.
*/
Node<T> reverse(Node<T> node)
{
Node<T> prev = null;
Node<T> current = node;
Node<T> next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// function to check the linkedlist is either empty or not
public boolean isEmpty() {
return head == null;
}
// function to give the sum of all the data in the nodes.
public int sum(Node<T> head) {
if(head.data instanceof String){
System.out.println("Cannot find sum of strings");
return 0;
}
int sum = 0;
Node<T> curr = head;
while (curr != null) {
sum += (int)curr.data;
curr = curr.next;
}
return sum;
}
public static void main(String args[]) {
// LinkedList<String> ll = new LinkedList<String>();
LinkedList<Integer> ll = new LinkedList<>();
ll.addLast(10);
ll.addLast(20);
ll.addLast(30);
ll.addLast(40);
ll.addFirst(50);
// ll.addLast("Hello");
// ll.addLast("i");
// ll.addLast("am");
// ll.addLast("Harsh Goyal");
System.out.println("Original List:");
ll.printList();
System.out.println("The sum of List is: " + ll.sum(ll.head));
ll.head = ll.reverse(ll.head);
System.out.println("Reversed List:");
ll.printList();
System.out.println("The list is empty: " + ll.isEmpty());
}
}