合并两个连续的单链表
class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode temp = new ListNode(0);ListNode cur = temp;while(list1 != null && list2 != null) {if(list1.val > list2.val) {cur.next = list2;list2 = list2.next;} else {cur.next = list1;list1 = list1.next;}cur = cur.next;}cur.next = list2 == null ? list1 : list2;return temp.next;}}
class Solution { public ListNode mergeTwoLists(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; } } }删除排序链表中重复元素
class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode cur = head; while(cur != null && cur.next != null) { if(cur.val == cur.next.val) { cur.next = cur.next.next; } else { cur = cur.next; } } return head; } }
