Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example: 1 2 Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
JAVA题解: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 package algorithm.c3;public class Leetcode21 { public ListNode mergeTwoLists (ListNode l1, ListNode l2) { ListNode l = new ListNode(0 ); ListNode cur = l; while (l1.next != null || l2.next != null ){ if (l1.next == null ) { cur.next = l2; } else if (l2.next == null ) { cur.next = l1; }else { if (l1.val > l2.val){ cur.next= l2; l2 = l2.next; }else if (l1.val == l2.val){ cur.next= l2; cur.next.next = l1; l1 = l1.next; l2 = l2.next; }else { cur.next = l1; l1 = l1.next; } } cur = cur.next; } return l.next; } public ListNode mergeTwoLists1 (ListNode l1, ListNode l2) { ListNode prehead = new ListNode(-1 ); ListNode prev = prehead; while (l1 != null && l2 != null ) { if (l1.val <= l2.val) { prev.next = l1; l1 = l1.next; } else { prev.next = l2; l2 = l2.next; } prev = prev.next; } prev.next = l1 == null ? l2 : l1; return prehead.next; } public ListNode mergeTwoLists2 (ListNode l1, ListNode l2) { if (l1 == null ) { return l2; } else if (l2 == null ) { return l1; } else if (l1.val < l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l1, l2.next); return l2; } } public static void main (String[] args) { ListNode l1 = new ListNode(1 ); l1.next = new ListNode(2 ); l1.next.next = new ListNode(4 ); ListNode l2 = new ListNode(1 ); l2.next = new ListNode(1 ); l2.next.next = new ListNode(3 ); System.out.println(new Leetcode21().mergeTwoLists1(l1,l2)); } public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } }
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example: 1 2 3 4 5 6 7 8 9 For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]
JAVA题解: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 package algorithm.c3;import java.util.LinkedList;import java.util.List;public class Leetcode22 { public List<String> generateParenthesis (int n) { LinkedList<LinkedList<String>> result = new LinkedList<LinkedList<String>>(); if (n == 0 ) return result.get(0 ); LinkedList<String> list0 = new LinkedList<String>(); list0.add("" ); result.add(list0); LinkedList<String> list1 = new LinkedList<String>(); list1.add("()" ); result.add(list1); for (int i = 2 ; i <= n; i++) { LinkedList<String> temp = new LinkedList<String>(); for (int j = 0 ; j < i; j++) { List<String> str1 = result.get(j); List<String> str2 = result.get(i - 1 - j); for (String s1 : str1) { for (String s2 : str2) { String el = "(" + s1 + ")" + s2; temp.add(el); } } } result.add(temp); } return result.get(n); } }