File tree 7 files changed +238
-0
lines changed
DataAndAlgoL/Chp3LinkedLists
LeetCode/Grind169/LinkedLists 7 files changed +238
-0
lines changed Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+ /*
3
+ * HOW TO CHECK IS LINKED LIST IS PALINDROME OR NOT
4
+ *
5
+ * ALGORITHM:
6
+ * 1. GET THE MIDDLE OF THE LINKED LIST
7
+ * 2. REVERSE THE SECOND HALF OF THE LINKED LIST
8
+ * 3.COMPARE THE 1ST HALF WITH THE 2ND HALF
9
+ * 4. CONSTRUCT THE ORIGINAL LINKED LIST BY REVERSING THE SECOND HALF AGAIN AND ATTACHING IT BACK TO THE 1ST HALF
10
+ */
11
+ public class Problem39 {
12
+ public static void main (String [] args ) {
13
+
14
+ }
15
+
16
+
17
+ }
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+
3
+ /*
4
+ * Exchange the adjacent elements in a link list
5
+ */
6
+
7
+ public class Problem40 {
8
+ public static void main (String [] args ) {
9
+
10
+ }
11
+
12
+ public ListNode exchangeAdjNodes (ListNode head ){
13
+ ListNode temp =null ; // to hold another node for swap
14
+
15
+ temp .next =head ;
16
+ ListNode prev = temp ;
17
+ ListNode curr = head ;
18
+
19
+ while ( curr != null && curr .next != null ){
20
+ ListNode tmp = curr .next .next ;
21
+ curr .next .next = prev .next ;
22
+ prev .next = curr .next ;
23
+ curr .next = tmp ;
24
+ prev = curr ;
25
+ curr =prev .next ;
26
+ }
27
+
28
+ return temp .next ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ package Grind169 .LinkedLists ;
2
+
3
+ public class LinkedListCycle_141 {
4
+ public class ListNode {
5
+ int val ;
6
+ ListNode next ;
7
+ ListNode () {}
8
+ ListNode (int val ) { this .val = val ; }
9
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
10
+ }
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ }
15
+
16
+ public static boolean hasCycle (ListNode head ) {
17
+ ListNode slow =head ;
18
+ ListNode fast = head ;
19
+ if (head == null ){
20
+ return false ;
21
+ }
22
+ while (fast .next != null && fast .next .next != null ){
23
+ slow = slow .next ;
24
+ fast =fast .next .next ;
25
+
26
+ if (slow == fast ){
27
+ return true ;
28
+ }
29
+ }
30
+
31
+ return false ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ package Grind169 .LinkedLists ;
2
+
3
+ public class Merge2SortedLists_21 {
4
+
5
+
6
+ //Definition for singly-linked list.
7
+ public class ListNode {
8
+ int val ;
9
+ ListNode next ;
10
+ ListNode () {}
11
+ ListNode (int val ) { this .val = val ; }
12
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
13
+ }
14
+ public static void main (String [] args ) {
15
+
16
+ }
17
+
18
+ public static ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
19
+
20
+ if (l1 ==null ){
21
+ return l2 ;
22
+ }
23
+
24
+ if (l2 ==null ){
25
+ return l1 ;
26
+ }
27
+
28
+ if (l1 .val <l2 .val ){
29
+ l1 .next =mergeTwoLists (l1 .next , l2 );
30
+ return l1 ;
31
+ }else {
32
+ l2 .next = mergeTwoLists (l1 , l2 .next );
33
+ return l2 ;
34
+ }
35
+
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ package Grind169 .LinkedLists ;
2
+
3
+ public class MidOfLinkedList_876 {
4
+ public class ListNode {
5
+ int val ;
6
+ ListNode next ;
7
+ ListNode () {}
8
+ ListNode (int val ) { this .val = val ; }
9
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
10
+ }
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ }
15
+
16
+ public static ListNode middleNode (ListNode head ) {
17
+ ListNode slow = head ;
18
+ ListNode fast = head ;
19
+
20
+ while (fast != null && fast .next != null ){
21
+ slow = slow .next ;
22
+ fast = fast .next .next ;
23
+ }
24
+
25
+ return slow ;
26
+
27
+
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package Grind169 .LinkedLists ;
2
+
3
+ public class PalindromeLinkedList_234 {
4
+ public class ListNode {
5
+ int val ;
6
+ ListNode next ;
7
+ ListNode () {}
8
+ ListNode (int val ) { this .val = val ; }
9
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
10
+ }
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ }
15
+
16
+ /*\
17
+ * Use Floyeds cycle to find middle of the list
18
+ * reverse second half of the linked list
19
+ * compare both 1st half and 2nd half
20
+ */
21
+ public static boolean isPalindrome (ListNode head ) {
22
+ //Floyed cycle
23
+ ListNode slow =head ;
24
+ ListNode fast =head ;
25
+ // Swap temp nodes
26
+ ListNode prev ;
27
+ ListNode temp ;
28
+
29
+ //Find middle list using Floyed cycle
30
+ while (fast != null && fast .next != null ){
31
+ slow =slow .next ;
32
+ fast =fast .next .next ;
33
+ }
34
+
35
+ //
36
+ prev =slow ; // prev becomes middle node
37
+ slow = slow .next ; //slow becomes next node to middle node
38
+ prev .next =null ; // we assign the prev (mid node) next pointer to null
39
+
40
+ //We swap the nodes in the second half
41
+ while (slow != null ){
42
+ temp =slow .next ;
43
+ slow .next = prev ;
44
+ prev =slow ;
45
+ slow =temp ;
46
+ }
47
+
48
+ //fast goes to first node and slow goes to middle node
49
+ fast =head ;
50
+ slow =prev ;
51
+ //compare each node fast (from 1st half) and slow node (from 2nd half) check if they are equal
52
+ while (slow != null ){
53
+ if (fast .val != slow .val ){ //if nodes are no equal then list is not a palindrome and returns false
54
+ return false ;
55
+ }
56
+
57
+ fast =fast .next ;
58
+ slow = slow .next ;
59
+ }
60
+
61
+ return true ;
62
+ }
63
+ }
Original file line number Diff line number Diff line change
1
+ package Grind169 .LinkedLists ;
2
+
3
+ public class ReverseLinkedList_206 {
4
+ public class ListNode {
5
+ int val ;
6
+ ListNode next ;
7
+ ListNode () {}
8
+ ListNode (int val ) { this .val = val ; }
9
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
10
+ }
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ }
15
+
16
+ public static ListNode reverseList (ListNode head ) {
17
+ ListNode newHead =null ;
18
+
19
+ while (head != null ){
20
+ ListNode next = head .next ;
21
+ head .next = newHead ;
22
+ newHead =head ;
23
+ head =next ;
24
+ }
25
+
26
+ return newHead ;
27
+ }
28
+
29
+ }
You can’t perform that action at this time.
0 commit comments