标签:
1 /** 2 * LeetCode: Palindrome Linked List 3 * Given a singly linked list, determine if it is a palindrome. 4 * Could you do it in O(n) time and O(1) space? 5 * 6 * @author LuoPeng 7 * @time 2015.8.3 8 * 9 */ 10 public class Solution { 11 12 /** 13 * Reverse the second part, then compare the nodes one by one. 14 * Time: O(n) + O((n-1)/2) + O(n/2-1) + O(n/2) + O(1) = O(n) 15 * Space: Only 7 parameters are needed, so it is O(1) 16 * 17 * @param head the first node of a linked list 18 * @return true if it is a palindrome, false otherwise. 19 */ 20 public boolean isPalindrome(ListNode head) { 21 22 if ( head == null || head.next == null) {return true;} 23 24 ListNode middle = null; // the middle index of linked list 25 ListNode current = null; // the nodes need to be reversed 26 ListNode lastHaveReversed = null; // the last node that has been reversed 27 ListNode temp = head; 28 29 /* 30 * the size of linked list 31 * Time: O(n) 32 */ 33 int length = 0; 34 while (temp != null) { 35 temp = temp.next; 36 length++; 37 } 38 39 /* 40 * get the index of middle 41 * Time: O((n-1)/2) 42 */ 43 int i = 0, size; 44 middle = head; 45 size = (length-1)/2; 46 while ( i < size) { 47 middle = middle.next; 48 i++; 49 } 50 51 /* 52 * reverse the last half part, from middleNext to the end 53 * just like the insert-sort 54 * Time: O(n/2-1) 55 */ 56 size = length/2-1; // the number of nodes need to be reversed 57 lastHaveReversed = middle.next; 58 current = lastHaveReversed.next; 59 for ( i = 0; i < size; i++) { 60 lastHaveReversed.next = current.next; 61 current.next = middle.next; 62 middle.next = current; 63 current = lastHaveReversed.next; 64 } 65 66 /* 67 * judge 68 * Time: O(n/2) 69 */ 70 temp = head; // the start of first part 71 middle = middle.next; // the start of second part 72 for ( i = 0; i < length/2; i++) { 73 if ( temp.val == middle.val) { 74 temp = temp.next; 75 middle = middle.next; 76 } else { 77 break; 78 } 79 } 80 81 return i == length/2; 82 83 } 84 85 /** 86 * For every node in the first part, find the other one in the second part and compare the value 87 * Time: O(n) 88 * Space: O(1) 89 * 90 * @param head 91 * @return 92 */ 93 public boolean isPalindrome2(ListNode head) { 94 if ( head == null || head.next == null) { 95 return true; 96 } else { 97 ListNode temp = head; 98 int length = 0; 99 while (temp != null) { 100 temp = temp.next; 101 length++; 102 } 103 104 int i, j; 105 for ( i = 0; i < length/2; i++) { 106 j = i; 107 temp = head; 108 while (j < length-1-i) { 109 temp = temp.next; 110 j++; 111 } 112 if ( head.val != temp.val) { 113 break; 114 } 115 head = head.next; 116 } 117 return i == length/2; 118 } 119 } 120 121 }
1 /** 2 * LeetCode: Implement Queue using Stacks 3 * 4 * @author LuoPeng 5 * @time 2015.8.3 6 * 7 */ 8 9 class MyQueue { 10 // Push element x to the back of queue. 11 public void push(int x) { 12 main.push(x); 13 } 14 15 // Removes the element from in front of queue. 16 public void pop() { 17 int size = main.size(); 18 for ( int i = 0; i < size-1; i++ ) { 19 help.push(main.peek()); 20 main.pop(); 21 } 22 main.pop(); 23 for ( int i = 0; i < size-1; i++) { 24 main.push(help.peek()); 25 help.pop(); 26 } 27 } 28 29 // Get the front element. 30 public int peek() { 31 int size = main.size(); 32 for ( int i = 0; i < size-1; i++ ) { 33 help.push(main.peek()); 34 main.pop(); 35 } 36 int value = main.peek(); 37 for ( int i = 0; i < size-1; i++) { 38 main.push(help.peek()); 39 help.pop(); 40 } 41 return value; 42 } 43 44 // Return whether the queue is empty. 45 public boolean empty() { 46 return main.empty(); 47 } 48 49 private MyStack main = new MyStack(); 50 private MyStack help = new MyStack(); 51 } 52 53 class MyStack { 54 public void push(int x) { 55 values.add(x); 56 } 57 public void pop() { 58 values.remove(values.size()-1); 59 } 60 public int peek() { 61 return values.get(values.size()-1); 62 } 63 public boolean empty() { 64 return values.size() == 0; 65 } 66 public int size() { 67 return values.size(); 68 } 69 70 private List<Integer> values = new ArrayList<Integer>(); 71 }
标签:
原文地址:http://www.cnblogs.com/luop/p/4700192.html