Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23f114b

Browse files
Add implementations for checking if a number is a palindrome, retrieving the nth node from the end of a linked list, and reversing a linked list
1 parent 7af65c8 commit 23f114b

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.LinkedList;
2+
import java.util.Scanner;
3+
4+
public class CheckLinkedListPalindrome {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.print("Enter the number: ");
8+
int num = sc.nextInt();
9+
10+
boolean result = isPalindrome(num);
11+
if (result) {
12+
System.out.println("The number is a palindrome: " + num);
13+
} else {
14+
System.out.println("The number is not a palindrome: " + num);
15+
}
16+
}
17+
18+
static boolean isPalindrome(int num){
19+
LinkedList <Integer> list = new LinkedList<>();
20+
int originalNum = num;
21+
22+
//step 1
23+
while(num > 0){
24+
int digit = num %10;
25+
list.add(digit);
26+
num = num/10;
27+
}
28+
29+
//step 2
30+
int start = 0;
31+
System.out.println( "start" +start);
32+
int end = list.size() -1;
33+
System.out.println("end" +end);
34+
// System.out.println(list.size());
35+
while(start < end){
36+
if(!list.get(start).equals(list.get(end))){
37+
return false;
38+
}
39+
start ++;
40+
end --;
41+
}
42+
return true;
43+
}
44+
45+
}

‎LinkedList/FirstExample.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import java.util.LinkedList;
3+
4+
class FirstExample{
5+
public static void main(String[] args) {
6+
LinkedList <Integer> myList = new LinkedList<>();
7+
LinkedList<String> strList = new LinkedList<>();
8+
9+
myList.add(10);
10+
myList.add(20);
11+
12+
System.out.println(myList);
13+
14+
myList.addFirst(100);
15+
System.out.println("After Adding: " +myList);
16+
17+
myList.remove(2);
18+
System.out.println("Remove the 2 index " +myList);
19+
20+
myList.remove();
21+
System.out.println("After Remove " +myList);
22+
23+
24+
System.out.println(" ");
25+
26+
strList.add("My");
27+
strList.add("Name");
28+
strList.add("is");
29+
strList.add("Ronaldo");
30+
31+
32+
for (String str : strList) {
33+
System.out.print(str + " ");
34+
}
35+
36+
System.out.println();
37+
}
38+
}

‎LinkedList/NthNodeFromLast.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import java.util.LinkedList;
3+
import java.util.Scanner;
4+
5+
public class NthNodeFromLast {
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
System.out.print("Enter the nth position form last: " );
9+
int n = sc.nextInt();
10+
LinkedList <Integer> list = new LinkedList<>();
11+
12+
list.add(10);
13+
list.add(20);
14+
list.add(30);
15+
list.add(40);
16+
list.add(50);
17+
list.add(60);
18+
19+
20+
int size = list.size();
21+
22+
23+
if(n > size || n <= 0){
24+
System.out.println("Invalid position you enterd...");
25+
26+
}else{
27+
int value = list.get(size-n);
28+
System.out.println("The " + n + "th node from the end is: " + value);
29+
}
30+
31+
}
32+
}

‎LinkedList/ReverseLinkedList.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import java.util.Collections;
3+
import java.util.LinkedList;
4+
5+
public class ReverseLinkedList {
6+
public static void main(String[] args) {
7+
LinkedList <Integer> myList = new LinkedList<>();
8+
9+
myList.add(10);
10+
myList.add(20);
11+
myList.add(30);
12+
System.out.println("Before Reverse : " + myList);
13+
14+
Collections.reverse(myList);
15+
System.out.println("After Reverse: " +myList);
16+
17+
18+
System.out.println("Size of the LinkedList: " +myList.size());
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /