Three Sum - Leet Code Solution
Problem Statement Given an array nums of n integers, are there elements a, b, c…
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 Given an array nums of n integers, are there elements a, b, c…
Its a kind of incremental insertion technique, where the algorithm build up…
Problem Statement Maximum Length of Subarray With Positive Product. Given an…
Problem Statement Given an array of integers, return indices of the two numbers…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement You are given a string text of words that are placed among…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…