Sunteți pe pagina 1din 8

REMOVE AN ELEMENT FROM LINKED LIST

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
*}
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode node= new ListNode(0);
node.next = head;
head = node;
ListNode curr= new ListNode(0);
curr= head;
ListNode temp= new ListNode(0);
while (curr.next != null) {
temp= curr.next;
if (temp.val == val)
curr.next = temp.next;
else
curr = curr.next;
}
return node.next;
}
}

VALID PARENTHESIS
class Solution {
public boolean isValid(String s) {
char[] array = s.toCharArray();
int indesk = 0;
int i = 1;
int num = 0;
while (i < array.length) {

while (indesk >= 0 && i < array.length && (array[indesk] == '(' && array[i] == ')' ||
array[indesk] == '['&& array[i] == ']' || array[indesk] == '{'&& array[i] == '}')) {
array[i] = ' ';
array[indesk] = ' ';
num += 2;
indesk -= 1;
while (indesk >= 0 && array[indesk] == ' ')
indesk -= 1;
i += 1;
}
if (i != array.length) {
indesk = i;
i += 1;
}
}
return num == array.length;
}
REVERSE POLISH NOTATION

class Solution {
public int evalRPN(String[] tokens) {
int returnValue = 0;
String ops = "+-*/";

Stack<String> stack = new Stack<String>();

for (String t : tokens) {


if (!ops.contains(t)) {
stack.push(t);
} else {//pop numbers from stack if it is an operator
int a = Integer.valueOf(stack.pop());
int b = Integer.valueOf(stack.pop());
switch (t) {
case "+":
stack.push(String.valueOf(a + b));
break;
case "-":
stack.push(String.valueOf(b - a));
break;
case "*":
stack.push(String.valueOf(a * b));
break;
case "/":
stack.push(String.valueOf(b / a));
break;
}
}
}

returnValue = Integer.valueOf(stack.pop());

return returnValue;
}
}

REVERSE LINKED LIST

class Solution {
public ListNode reverseList(ListNode head) {

if(head == null)
{
return null;
}

ListNode temp = null;


ListNode current = head.next;
ListNode nextNode;

while(head.next !=null)
{
head.next = temp;
temp = head;
head = current;
current = current.next;
}

head.next = temp;
return head;
}
}

ROTATE LIST

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
*}
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null) return null;
int size = 1;
ListNode fast_point = head;
ListNode slow_point = head;
while(fast_point.next != null){
size++;
fast_point = fast_point.next;
}
for(int i=1; i < size - k % size; i++)
slow_point = slow_point.next;
fast_point.next = head;
head = slow_point.next;
slow_point.next = null;
return head;
}
}

MERGE LINKED LISTS

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
*}
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode p=head;
ListNode 1pointer=l1;
ListNode 2pointer=l2;
while(1pointer!=null && 2pointer!=null){
if(1pointer.val < 2pointer.val){
p.next = 1pointer;
1pointer = 1pointer.next;
}else{
p.next = 2pointer;
2pointer = 2pointer.next;
}
p=p.next;
}
if(1pointer!=null){
p.next = 1pointer;
}
if(2pointer!=null){
p.next = 2pointer;
}
return head.next;
}
}

DESIGN LINKED LIST

class MyLinkedList {
ListNode dummy;
ListNode tail;
int length;
/** Initialize your data structure here. */
public MyLinkedList() {
dummy = new ListNode(-1);
tail = dummy;
length = 0;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index == -1 || index >= length){
return -1;
}
ListNode cur = dummy;
while(index >= 0){
cur = cur.next;
index--;
}
return cur.val;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the
new node will be the first node of the linked list. */
public void addAtHead(int val) {
ListNode head = new ListNode(val);
head.next = dummy.next;
dummy.next = head;
length++;
if(tail.next != null){
tail = tail.next;
}
}

/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
tail.next = new ListNode(val);
tail = tail.next;
length++;
}

/** Add a node of value val before the index-th node in the linked list. If index equals to the
length of linked list, the node will be appended to the end of linked list. If index is greater than
the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if(index > length){
return;
}
ListNode node = new ListNode(val);
ListNode cur = dummy;
while(index > 0){
cur = cur.next;
index--;
}
node.next = cur.next;
cur.next = node;
length++;
if(tail.next != null){
tail = tail.next;
}
}

/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index < 0||index >= length){
return;
}

ListNode cur = dummy;


while(index > 0){
cur = cur.next;
index--;
}
if(cur.next == tail){
tail = cur;
}
cur.next = cur.next.next;
length--;

}
}

/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/

BACKSPACE COMPARE

class Solution {
public boolean backspaceCompare(String S, String T) {
int i = S.length()-1;
int j = T.length()-1;

while(i>=0 || j>=0){
int c1=0;
while(i>=0 && (c1>0 || S.charAt(i)=='#')){
if(S.charAt(i)=='#'){
c1++;
}else{
c1--;
}

i--;
}

int c2=0;
while(j>=0 && (c2>0 || T.charAt(j)=='#')){
if(T.charAt(j)=='#'){
c2++;
}else{
c2--;
}

j--;
}

if(i>=0 && j>=0){


if(S.charAt(i)!=T.charAt(j)){
return false;
}else{
i--;
j--;
}
}else{
if(i>=0 || j>=0){
return false;
}
}
}

return i<0 && j<0;


}
}

S-ar putea să vă placă și