Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
guolinaileen / Pow(x, n).java
Last active March 29, 2020 23:17
binary strategy: 2^n=2^(n/2) * 2^(n/2) * 2^(n%2)
public class Solution {
public double pow(double x, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(n==0) return 1;
if(x==0) return 0;
if(n==1) return x;
if(n<0)
{
n=-n; x=1/x;
@guolinaileen
guolinaileen / Swap Nodes in Pairs.java
Last active December 26, 2019 08:24
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }