力扣算法题1

1.反转链表

public ListNode ReverseList (ListNode head) {

if (head==null ){

return null ;

}

ListNode newHead = new ListNode(-1);

ListNode cur = head;

while (cur!=null ){

ListNode next = cur.next;

cur.next = newHead.next;

newHead.next = cur;

cur = next;

}

return newHead.next;

}

public ListNode ReverseList (ListNode head) {

if(head==null||head.next==null){

return head;

}

ListNode newHead = ReverseList(head.next);

head.next.next = head;

head.next = null;

return newHead;

}

指定区间内反转链表

public ListNode reverse(ListNode head,ListNode end){

if(head==null||head.next ==null)

return head;

ListNode newHead = new ListNode(-1);

ListNode cur = head;

while(cur!=end){

ListNode next = cur.next;

cur.next = newHead.next;

newHead.next = cur;

cur = next;

}

return newHead.next;

}

public ListNode reverseBetween (ListNode head, int m, int n) {

ListNode newHead = new ListNode(-1);

newHead.next = head;

ListNode prev = newHead;

ListNode cur = head;

for(int i = 0;i <m-1;i++){

prev = prev.next;

cur = cur.next;

}

ListNode next = head;

ListNode tmp = cur;

for(int i = 0;i<n;i++){

next = next.next;

}

ListNode ret = reverse(cur,next);

prev.next = ret;

tmp.next = next;

return newHead.next;

}

  1. 合并两个排序的链表(2种方法)

public ListNode Merge (ListNode pHead1, ListNode pHead2) {

ListNode newHead = new ListNode(-1);

ListNode tail = newHead;

while(pHead1!=null&&pHead2!=null){

if(pHead1.val<pHead2.val){

tail.next = pHead1;

pHead1=pHead1.next;

tail=tail.next;

}else{

tail.next = pHead2;

pHead2=pHead2.next;

tail=tail.next;

}

}

while(pHead1!=null){

tail.next=pHead1;

pHead1=pHead1.next;

tail=tail.next;

}

while(pHead2!=null){

tail.next=pHead2;

pHead2=pHead2.next;

tail=tail.next;

}

return newHead.next;

}

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

if(list1==null)

return list2;

if(list2==null)

return list1;

if(list1.val<list2.val){

list1.next = mergeTwoLists(list1.next,list2);

return list1;

}else{

list2.next = mergeTwoLists(list1,list2.next);

return list2;

}

}

4.合并k个已排序的链表(2种方法)

public ListNode mergeKLists (ArrayList<ListNode> lists){

return merge(lists,0,lists.size()-1);

}

public ListNode merge(ArrayList<ListNode> lists,int left, int right){

if(left>right)

return null;

if(left==right)

return lists.get(left);

int mid = (left+right)/2;

ListNode l1 = merge(lists,left,mid);

ListNode l2 = merge(lists,mid+1,right);

return Merge(l1,l2);

}

public ListNode mergeKLists2 (ArrayList<ListNode> lists){

PriorityQueue<ListNode> heap = new PriorityQueue<>((v1,v2)->v1.val-v2.val);

for(ListNode l:lists){

if(l!=null)

heap.offer(l);

}

ListNode newHead = new ListNode(-1);

ListNode cur = newHead;

while(!heap.isEmpty()){

ListNode node = heap.poll();

cur.next = node;

cur = cur.next;

if(node.next!=null)

heap.offer(node.next);

}

return newHead.next;

}

//=======================================================

public ListNode Merge (ListNode pHead1, ListNode pHead2) {

ListNode newHead = new ListNode(-1);

ListNode tail = newHead;

while(pHead1!=null&&pHead2!=null){

if(pHead1.val<pHead2.val){

tail.next = pHead1;

pHead1=pHead1.next;

tail=tail.next;

}else{

tail.next = pHead2;

pHead2=pHead2.next;

tail=tail.next;

}

}

while(pHead1!=null){

tail.next=pHead1;

pHead1=pHead1.next;

tail=tail.next;

}

while(pHead2!=null){

tail.next=pHead2;

pHead2=pHead2.next;

tail=tail.next;

}

return newHead.next;

}

5.判断是否有环

public boolean hasCycle(ListNode head) {

if(head==null){

return false;

}

ListNode fast = head;

ListNode slow = head;

while(fast!=null&&fast.next!=null){

fast = fast.next.next;

slow = slow.next;

if(fast == slow){

return true;

}

}

return false;

}