Posted onInLeetcode
,
Linked List Symbols count in article: 989Reading time ≈1 mins.
Question
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Posted onInLeetcode
,
Linked List Symbols count in article: 807Reading time ≈1 mins.
Question
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Posted onInLeetcode
,
Linked List Symbols count in article: 1.5kReading time ≈1 mins.
Question
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list’s nodes, only nodes themselves may be changed.
classSolution{ public ListNode reverseKGroup(ListNode head, int k){ // Save the previous reversed pointer in prev //and in wach iteration use prev.next to set the previous ptr //to the current reversed. ListNode tempNode = new ListNode(0); tempNode.next = head; ListNode tempHead = head; ListNode prev = tempNode; while(tempHead!=null){ // Starting of each reversed list, //it will become the last after reversing ListNode klast = tempHead; int num = k; // Jump k while(num>0 && tempHead!=null){ tempHead = tempHead.next; num--; } // If cannot reverse if(num!=0) { prev.next = klast; break; } // start of each reversed group ListNode kstart = reverse(klast,k); // Use previous's next to point to curr reversed prev.next = kstart; // Set prev to current rev end. prev = klast; } return tempNode.next; } // Standard reverse code public ListNode reverse(ListNode head, int k){ ListNode prev = null; while(head!=null && k>0){ ListNode next = head.next; head.next = prev; prev = head; head = next; k--; } return prev; } }
Posted onInLeetcode
,
Linked List Symbols count in article: 732Reading time ≈1 mins.
Question
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
classSolution{ public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res = new ArrayList<>(); dfs(res, new ArrayList<>(), n, k, 1); return res; } privatevoiddfs(List<List<Integer>> res, List<Integer> level, int n, int k, int index){ if (level.size() == k) res.add(new ArrayList<>(level)); else{ for (int i = index; i <= n; i++){ level.add(i); dfs(res, level, n, k, i + 1); level.remove(level.size() - 1); } } } }
Posted onInLeetcode
,
Backtracking Symbols count in article: 1.1kReading time ≈1 mins.
Question
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
public List<List<Integer>> combinationSum2(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; }
Posted onInLeetcode
,
Backtracking Symbols count in article: 1.3kReading time ≈1 mins.
Question
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations ofcandidateswhere the chosen numbers sum totarget. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
classSolution{ public List<List<Integer>> combinationSum(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; }
privatevoidbacktrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ if(remain < 0) return; elseif(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elements tempList.remove(tempList.size() - 1); } } } } /* Time: O(k * C(n, k)), O(n*2^n) Space: O(n) */
public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, newboolean[nums.length]); return list; }