ArrayList and LinkedList both implement the List interface and maintain insertion order. Both are non-synchronized classes. However, there are many differences between the ArrayList and LinkedList classes that are given below.
Convert Sorted List to Binary Search Tree
This is No.109 in leetcode. Below is the solution: /** * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int * x) { val = x; } } */ /** * Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode * right; TreeNode(int x) { …
Continue reading “Convert Sorted List to Binary Search Tree”
Reverse Linked List II
This is No.92 in leetcode. The problem description: Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. I have a wrong idea on this problem, so I post the reference solution below: class Solution { // Object level variables since we …
Remove Duplicates from Sorted List II
This is No.82. in leetcode. My code is given below: /** * 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 …
Rotate List
This is No.61 in leetcode. The description: Given a linked list, rotate the list to the right by k places, where k is non-negative. This is my original solution: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = …