diff --git a/src/main/java/Rotate Image b/src/main/java/Rotate Image new file mode 100644 index 0000000..15fad2a --- /dev/null +++ b/src/main/java/Rotate Image @@ -0,0 +1,50 @@ +-------------------------------------------------------------- +@Sayak +Rotate the image by 90 degrees (clockwise). +Given input matrix = +[ + [ 8, 1, 9,12], + [ 2, 4, 8,10], + [13, 3, 6, 7], + [18,14,12,16] +], + +rotate the input matrix in-place such that it becomes: +[ + [18,13, 2, 8], + [14, 3, 4, 1], + [12, 6, 8, 9], + [16, 7,10,12] +] + + The solution can be achieved int two steps: + 1) Transpose the matrix + 2) Reverse the matrix rows start<->end , start++, end-- + +-------------------------------------------------------------- + +class Solution { + public void rotate(int[][] matrix) { + + int n= matrix.length; + for(int i=0;i2 +Output: false + +Example 2: +Input: 1->2->2->1 +Output: true +----------------------------------------------------------- + + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public boolean isPalindrome(ListNode head) { + + ListNode slow=head; + ListNode fast=head; + + while(fast!=null && fast.next!=null) + { + fast=fast.next.next; + slow=slow.next; + } + slow=reversed(slow); + fast=head; + + + while(slow!=null) + { + if(slow.val!=fast.val) + { + return false; + } + slow=slow.next; + fast=fast.next; + } + return true; + + } + public ListNode reversed(ListNode head) + { + ListNode pnode=null; + ListNode nnode=head; + while(head!=null) + { + nnode=nnode.next; + head.next=pnode; + pnode=head; + head=nnode; + } + return pnode; + } + +}