链表题

class Solution{
	public ListNode removeNthFromEnd(ListNode head,int n){
		if(head==null){
			return head;//判空
		}
		ListNode dumy=new ListNode(0);//创建虚拟头节点
		dummy.next=head;//虚拟头结点指向
		ListNode slow=dummy,fast=dummy;//定义两个指针分别指向虚拟头结点
		//让fast往后走n步,然后让first和slow往后面走,当fast走到最后一个点的时候终止
		for(int i=0;i<n;i++){
			fast=fast.next;
			
		}
		while(fast.next !=null){
			fast=fast.next;
			slow=slow.next;
		}
		
		//再把slow指出来的下一个点删掉
		slow.next=slow.next.next;
		//返回虚拟头结点的下一个点
		return dummy.next;
	}
}

链表题_第1张图片

class Solution{
	public void deleteNode(ListNode node){
		node.val=node.next.val;//把当前点伪装成下一个点
		node.next=node.next.next;//再把下一个点删掉
	}
}

链表题_第2张图片
链表题_第3张图片

class solution{
	public ListNode rotate(ListNode head){
		if(head==null)return null;//判空
		int size=1;
		ListNode fast=head,slow=head;//定义两个结点,分别指向头结点
		while(fast.next!=null){
			size++;
			fast=fast.next;//当下一个点不为空时,fast先走一步
		}
		for(int size-k%size;i>1;i--){
			slow=slow.next;//slow也开始往后走
		}
		fast.next=head;//当上面的for循环结束后,fast指向头节点
		head=slow.next;//头结点指向slow的下一节点
		slow.next=null;//slow.next指向空
	}
}

链表题_第4张图片

class Solution{
	public ListNode swapPairs(ListNode head){
		ListNode dummy=new ListNode(0);//建立虚拟结点
		dummy.next=head;//虚拟节点指向头结点
		ListNode start=dummy;//开始节点指向虚拟节点
		while(start.next !=null&& start.next.next !=null){//当开始节点的下两个都不为空时
			ListNode first=start.next;//first先走
			ListNode second=start.next.next;//second走
			start.next=second;
			first.next=second.next;//交互
			second.next=first;//交互
			start.first;
		}
		return dummy.next;
	}
}

链表题_第5张图片

class Solution{
	public ListNode reverseList(ListNode head){
		ListNode prev=null;
		while(head !=null){
			ListNode next=head.next;
			head.next=prev;
			prev=head;
			head=next;//a=b,b=c的意思
		}
		return prev;
	}
}

链表题_第6张图片链表题_第7张图片

import java.util.PriorityQueue;

/**
 *
 * Sort a linked list in O(n log n) time using constant space complexity.
 * 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
 *
 */

public class SortList {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args){
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        n4.next = n2;
        n2.next = n1;
        n1.next = n3;
        ListNode head = sortList(n4);
        System.out.print(head.val);
        head = head.next;
        while(head != null){
            System.out.print("," + head.val);
            head = head.next;
        }
        System.out.println("");
    }

    //堆排序
    public static ListNode sortList(ListNode head) {
        PriorityQueue<ListNode> minheap = new PriorityQueue<>((l1, l2) -> Integer.compare(l1.val, l2.val));
        ListNode p = head;
        while(p != null){
            minheap.add(p);
            p = p.next;
        }

        if(!minheap.isEmpty())
            p = minheap.poll();
        ListNode newhead = p;
        while(!minheap.isEmpty()){
            p.next = minheap.poll();
            p = p.next;
            //若不加此句,链表可能会成环并无限运行
            p.next = null;
        }

        return newhead;
    }

    //归并排序:最佳
    public static ListNode sortList1(ListNode head){
        if(head == null || head.next == null)
            return head;

        //快慢指针找中点
        ListNode fast = head.next.next;
        ListNode low = head;
        while(fast != null && fast.next != null){
             fast = fast.next.next;
             low = low.next;
        }

        //递归调用(非常重要:递归排序右半部,后将左右截断)
        ListNode l1 = sortList1(low.next);
        low.next = null;
        ListNode l2 = sortList1(head);

        //归并排序
        ListNode newhead = new ListNode(0);
        fast = newhead;
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                fast.next = l1;
                l1 = l1.next;
            }else{
                fast.next = l2;
                l2 = l2.next;
            }
            fast = fast.next;
        }
        fast.next = l1 == null ? l2 : l1;

        return newhead.next;
    }

    //快速排序
    public static ListNode sortList2(ListNode head){
        return QuickSort(head, null);
    }

    public static ListNode QuickSort(ListNode begin, ListNode end){
        if(begin == end || begin.next == end)
            return begin;

        ListNode head = new ListNode(0);
        ListNode smaller = head;
        ListNode partition = begin;
        ListNode bigger = begin;

        while(begin.next != end){
            if(begin.next.val < partition.val){
                smaller.next = begin.next;
                smaller = smaller.next;
                begin.next = begin.next.next;
            }else{
                begin = begin.next;
            }
        }
        smaller.next = bigger;

        ListNode left = QuickSort(head.next, partition.next);
        ListNode right = QuickSort(partition.next, end);
        head.next = left;
        while(left.next != null)
            left = left.next;
        left = right;

        return head.next;
    }

    //插入排序
    public ListNode insertionSortList(ListNode head) {
        if(head == null){
            return head;
        }
        
        ListNode newhead = new ListNode(0);
        ListNode cur = head;
        ListNode pre = newhead;
        while(cur != null){
            ListNode next = cur.next;
            while(pre.next != null && pre.next.val < cur.val){
                pre = pre.next;
            }
            //将cur插入pre.next
            cur.next = pre.next;
            pre.next = cur;
            pre = newhead;
            cur = next;
        }
        return newhead.next;        
    }
}


链表题_第8张图片

你可能感兴趣的:(数据结构与算法)