From 2bab253e9c2a876d555702c8effe4b8d5b8056b0 Mon Sep 17 00:00:00 2001 From: weiweijun Date: Wed, 13 Jan 2016 11:47:15 -0800 Subject: [PATCH 1/2] no message --- weijun li/hw1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 weijun li/hw1.txt diff --git a/weijun li/hw1.txt b/weijun li/hw1.txt new file mode 100644 index 0000000..e69de29 From 6664120dc20e5b07406be50c650fa12ee4452697 Mon Sep 17 00:00:00 2001 From: weiweijun Date: Wed, 13 Jan 2016 16:34:53 -0800 Subject: [PATCH 2/2] no message --- weijun li/hw1 | 118 ++++++++++++++++++++++++++++++++++++++++++++++ weijun li/hw1.txt | 0 2 files changed, 118 insertions(+) create mode 100644 weijun li/hw1 delete mode 100644 weijun li/hw1.txt diff --git a/weijun li/hw1 b/weijun li/hw1 new file mode 100644 index 0000000..c71ad1e --- /dev/null +++ b/weijun li/hw1 @@ -0,0 +1,118 @@ + //Remove Duplicates from Sorted List II + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } +*/ +public class Solution { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode dummy = new ListNode(0); + dummy.next = head; + head = dummy; + + ListNode pre = head; + while ((pre.next != null) && (pre != null)) { //traverse the whole list + ListNode p = pre.next; // ListNode P 必须在while里面,因为p的位置需要根据pre当前所在位置判断 + while (p.next != null && p.val == p.next.val) { //这里 && 前后一定不能换位置,必须先判断pre.next != null + p = p.next; //when finding next.val = next.next.val, we move the p reference + } + if (p != pre.next) { // find where pre.next should go + pre.next = p.next; + } + else { + pre = pre.next; + } + } + return dummy.next; + } + +} + + + + +//Reverse Linked List II +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +public class Solution { + public ListNode reverseBetween(ListNode head, int m, int n) { + if (m >= n || head == null) { + return head; + } + ListNode newHead = new ListNode(0); + newHead.next = head; + head = newHead; + + for (int i = 1; i < m; i++){ + if (head == null) { + return null; + } + head = head.next; + } + + ListNode premNode = head; + ListNode mNode = head.next; + ListNode nNode = mNode; + ListNode postnNode = mNode.next; + for (int i = m; i < n; i++) { + if (postnNode == null) { + return null; + } + ListNode temp = postnNode.next; + postnNode.next = nNode; + nNode = postnNode; + postnNode = temp; + } + premNode.next = nNode; + mNode.next = postnNode; + + return newHead.next; + } +} + + + +// Merge Two Sorted Lists + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +public class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode result = null; + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + + if (l1.val < l2.val) { + result = l1; + result.next = mergeTwoLists(l1.next, l2); + } + else { + result = l2; + result.next = mergeTwoLists(l1, l2.next); + } + return result; + + } +} \ No newline at end of file diff --git a/weijun li/hw1.txt b/weijun li/hw1.txt deleted file mode 100644 index e69de29..0000000