Maximum Subarray Problem
Problem Statement You are given an array of integers. And, you have find the…
September 16, 2019
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list’s nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
ListNode t = this;
while (t != null) {
sb.append(t.val).append(" -> ");
t = t.next;
}
return sb.toString();
}
}
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode first = head;
ListNode second = head.next;
ListNode prev = null;
ListNode result = second;
while (first != null && second != null) {
first.next = second.next;
second.next = first;
if (prev != null) {
prev.next = second;
}
prev = first;
first = first.next;
if (first != null) {
second = first.next;
}
}
return result;
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Swap Nodes in Pairs. Memory Usage: 34.5 MB, less than 100.00% of Java online submissions for Swap Nodes in Pairs.
Problem Statement You are given an array of integers. And, you have find the…
Problem Statement Given a string, find the length of the longest substring…
Problem Statement Roman numerals are represented by seven different symbols: I…
Young Tableau A a X b matrix is Young Tableau if all rows(from left to right…
This algorithm is very useful for large input. And, is quite efficient one. It…
Problem Statement Given a non-empty array of digits representing a non-negative…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…