学数据结构记录-1

单链表

单链表:值,next指针

public static class Node{
    public int value;
    public Node next;

    public Node(int data){
        value = data;
    }
}
反转
public static Node reverseLinkedList(Node head){
    Node pre = null;
    Node next = null;
    while (head != null){
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
}
实现队列
public static class MyQueue<V>{
    private Node<V> head;
    private Node<V> tail;
    private int size;

    public MyQueue(){
        head = null;
        tail = null;
        size = 0;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public int size(){
        return size;
    }

    public void offer(V value){
        Node<V> cur = new Node<V>(value);
        if(tail == null){
            head = cur;
            tail = cur;
        }else {
            tail.next = cur;
            tail = cur;
        }
        size++;
    }

    public V poll(){
        V ans = null;
        if(head != null){
            ans = head.value;
            head = head.next;
            size--;
        }else {
            tail = null;
        }
        return ans;
    }
    public V peek(){
        V ans = null;
        if(head != null){
            ans = head.value;
        }
        return ans;
    }
}
实现栈
public static class MyStack<V>{
    private Node<V> head;
    private int size;

    public MyStack(){
        head = null;
        size = 0;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public int size(){
        return size;
    }

    public void push(V value){
        Node<V> cur = new Node<V>(value);
        if(head == null){
            head = cur;
        }else {
            cur.next = head;
            head = cur;
        }
        size++;
    }

    public V pop(){
        V ans = null;
        if(head != null){
            ans = head.value;
            head = head.next;
            size--;
        }
        return ans;
    }
    public V peek(){
        return head != null ? head.value : null;
    }
}

双链表

双链表:last指针,值,next指针

public static class DoubleNode{
    public int value;
    public DoubleNode last;
    public DoubleNode next;

    public DoubleNode(int data){
        value = data;
    }
}
反转
public static DoubleNode reverseDoubleList(DoubleNode head){
    DoubleNode pre = null;
    DoubleNode next = null;
    while (head != null){
        next = head.next;
        head.next = pre;
        head.last = next;
        pre = head;
        head = next;
    }
    return pre;
}
实现双端队列
public static class MyDeque<V>{
    private DoubleNode<V> head;
    private DoubleNode<V> tail;
    private int size;

    public MyDeque(){
        head = null;
        tail = null;
        size = 0;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public int size(){
        return size;
    }

    public void pushHead(V value){
        DoubleNode<V> cur = new DoubleNode<V>(value);
        if(head == null){
            head = cur;
            tail = cur;
        }else {
            cur.next = head;
            cur.last = cur;
            head = cur;
        }
        size++;
    }
    public void pushTail(V value){
        DoubleNode<V> cur = new DoubleNode<V>(value);
        if(head == null){
            head = cur;
            tail = cur;
        }else {
            tail.next = cur;
            cur.last = tail;
            tail = cur;
        }
        size++;
    }

    public V pollHead(){
        V ans = null;
        if(head == null)
            return ans;
        size--;
        ans = head.value;
        if(head == tail){
            head = null;
            tail = null;
        }else {
            head = head.next;
            head.last = null;
        }
        return ans;
    }
    public V pollTail(){
        V ans = null;
        if(head == null)
            return ans;
        size--;
        ans = tail.value;
        if(head == tail){
            head = null;
            tail = null;
        }else {
            tail = tail.last;
            tail.next = null;
        }
        return ans;
    }

    public V peekHead(){
        V ans = null;
        if(head != null){
            ans = head.value;
        }
        return ans;
    }
    public V peekTail(){
        V ans = null;
        if(tail != null){
            ans = tail.value;
        }
        return ans;
    }
}

K个节点的组内逆序调整

eg.1-2-3-4-5-6-7-8-9 k=3
3-2-1-6-5-4-9-8-7

public static Node reverseKGroup(Node head,int k){
    Node start = head;
    Node end = getKGroupEnd(start,k);
    if(end == null){
        return head;
    }
    head = end;
    reverse(start,end);
    Node lastEnd = start;
    while (lastEnd.next != null){
        start = lastEnd.next;
        end = getKGroupEnd(start,k);
        if (end == null){
            return head;
        }
        reverse(start,end);
        lastEnd.next = end;
        lastEnd = start;
    }
    return head;
}
public static Node getKGroupEnd(Node start,int k){
    while(--k != 0 && start != null){
        start = start.next;
    }
    return start;
}

public static void reverse(Node start,Node end){
    end = end.next;
    Node pre = null;
    Node cur = start;
    Node next = null;
    while(cur != end){
        next = cur.next;
        cur.next = pre;
        pre = cur;
        cur = next;
    }
    start.next = end;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值