public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode listNode; if (Objects.isNull(l1) && Objects.isNull(l2)) { return null; } if (l1.val > l2.val) { listNode = new ListNode(l2.val); l2 = l2.next; } else { listNode = new ListNode(l1.val); l1 = l1.next; } ListNode first = listNode; while (Objects.nonNull(l1.next) || Objects.nonNull(l2.next)) { if (l1.val < l2.val) { listNode.next = new ListNode(l1.val); l1 = l1.next; } else { listNode.next = new ListNode(l2.val); l2 = l2.next; } listNode = listNode.next; } if (l1.val > l2.val) { listNode.next = new ListNode(l2.val); listNode = listNode.next; listNode.next = new ListNode(l1.val); } else { listNode.next = new ListNode(l1.val); listNode = listNode.next; listNode.next = new ListNode(l2.val); } return first; } |
2.
private List<Integer> path; private List<List<Integer>> paths; @Test void ttt() { int[][] arr = {{4,3,1}, {3,2,4}, {3}, {4}, {}}; test(arr); } // 4,3,1 3,2,4 3 4 [] public List<List<Integer>> test(int[][] graph) { path = new ArrayList<>(); paths = new ArrayList<>(); dfs(graph, 0); return paths; } public void dfs(int[][] graph, int current) { // 현재 포지션 저장 path.add(current); if (current == graph.length - 1) { paths.add(new ArrayList<>(path)); } else { for (final int i : graph[current]) { // 재귀 dfs(graph, i); } } // 수행 후 포지션 뒤로 path.remove(path.size() - 1); } |
3. fail
class Solution { List<Integer> result; public boolean isSameTree(TreeNode p, TreeNode q) { result = new ArrayList<>(); List<Integer> listP = search(p); result = new ArrayList<>(); List<Integer> listQ = search(q); return listP.equals(listQ); } private List<Integer> search(TreeNode node) { result.add(node.val); while (Objects.nonNull(node.left)) { search(node.left); } while (Objects.nonNull(node.right)) { search(node.right); } return result; } } |