publicboolHasCycle(ListNode head){if(head ==null)returnfalse;
HashSet<ListNode> bill =newHashSet<ListNode>();while(head !=null){if(bill.Contains(head)){returntrue;}else{
bill.Add(head);}
head = head.next;}returnfalse;}
删除排序数组中的重复项
publicintRemoveDuplicates(int[] nums){if(nums.Length ==0)return0;int i =0;for(int j =1; j < nums.Length; j++){if(nums[j]!= nums[i]){
i++;
nums[i]= nums[j];}}return i +1;}
合并两个有序链表
publicListNodeMergeTwoLists(ListNode l1,ListNode l2){ListNode head =newListNode(0);ListNode cur = head;while(l1 !=null&& l2 !=null){if(l1.val < l2.val){
cur.next = l1;
cur = l1;
l1 = l1.next;}else{
cur.next = l2;
cur = l2;
l2 = l2.next;}}if(l1 ==null){
cur.next = l2;}if(l2 ==null){
cur.next = l1;}return head.next;}
最大子序和
publicintMaxSubArray(int[] nums){if(nums.Length ==0)return0;int result = nums[0];int current = nums[0];for(int i =1; i < nums.Length; i++){if(current <0) current = nums[i];else current += nums[i];if(current >= result){
result = current;}}return result;}