Swap Nodes in Pairs
Swap Nodes in PairsLeetcode 24.
123456789101112131415161718192021222324252627282930/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode swapPairs(ListNode head) { //head = [1,2,3,4] ListNode newOne = new ...
Remove Nth Node From End of List
Remove Nth Node From End of ListLeetcode 19.
123456789101112131415161718192021222324252627282930313233343536/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode ...
Remove Duplicates from Sorted List
Remove Duplicates from Sorted ListLeetcode 83.
version 11234567891011121314151617181920212223/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode temp = head; while ...
Merge Two Sorted Lists
Merge Two Sorted ListsLeetcode 21.
12345678910111213141516171819202122232425/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1 == null) return l2; if( ...
Reverse Linked List
Reverse Linked ListLeetcode 206.
Version 1 Recursion123456789101112131415161718192021/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode reverseList(ListNode head) { if(head == null || head.next == null){ ...
Intersection of Two Linked Lists
Intersection of Two Linked ListsLeetcode 160.
lengthA = Linked List AlengthB = Linked List Bkey point : lengthA + lengthB = lengthB + lengthASo, Double pointers can be used here.
1234567891011121314151617181920212223/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode getIntersectionNode(ListNode hea ...
Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberLeetcode 17.
1234567891011121314151617181920212223242526272829303132333435class Solution { public List<String> letterCombinations(String digits) { List<String> combinations = new ArrayList<String>(); if (digits.length() == 0) { return combinations; } Map<Character, String> phoneMap = new HashMap<Character, String>() {{ put('2', "abc"); p ...
Sudoku Solver
Sudoku SolverLeetcode 37.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758class Solution { public void solveSudoku(char[][] board) { backtricking(board); } private boolean backtricking(char[][] board){ //for loop: outside for loop rows, inside for loop columns for(int i = 0; i < 9;i++){ //loop rows for(int j = 0; j < 9;j++){ //loop columns //Skip o ...
N Queens
N QueensLeetcode 51.
version 11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465class Solution { public List<List<String>> solveNQueens(int n) { List<List<String>> ans = new ArrayList<>(); if(n == 3){ return ans; } char[][] chessBoard = new char[n][n]; for(char[] c: chessBoard){ Arrays.fill(c,'.'); } backtricking( ...
Palindrome Partitioning
Palindrome PartitioningLeetcode 131.
123456789101112131415161718192021222324252627282930313233343536class Solution { public List<List<String>> partition(String s) { List<String> list = new ArrayList<>(); List<List<String>> ans = new ArrayList<>(); backtricking(list,s,ans); return ans; } public void backtricking(List<String> list, String s,List<List<String>> ans){ //recursion st ...