Funcionamiento básico (1) de una sola cadena de

1. Retire todos los elementos especificados en la lista
(https://leetcode-cn.com/problems/removelinked-list-elements/description/)

   public ListNode reverseList(ListNode head) {
        if(head == null) {
                return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode newhead = null;
        ListNode cur = head;
        ListNode prev = null;
        while(cur != null) {
            ListNode next = cur.next;
            if(next == null) {
                newhead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return newhead;
    }

2. Invertir una única lista enlazada
(https://leetcode-cn.com/problems/reverse-linkedlist/description/)

public ListNode removeElements(ListNode head, int val) {
    if(head == null) {
        return null;
    }
    ListNode prev = head;
    ListNode node = head.next;
    while(node != null) {
        if(node.val == val) {
            prev.next = node.next;
            node = prev.next;
        }else {
            prev = node;
            node = node.next;
        }
    }
    if(head.val == val) {
        head = head.next;
    }
    return head;
}

3. Dada una lista no vacía con una sola cabeza del primer nodo, una lista de nodos intermedio regresó.
(Https://leetcode-cn.com/problems/middle-of-the-linked-list/description/)

public ListNode middleNode(ListNode head) {
    int steps = size(head)/2;
    ListNode cur = head;
    for(int i = 0;i < steps;i++) {
        cur = cur.next;
    }
    return cur;
}
public int size(ListNode head) {
    int size = 0;
    for(ListNode cur = head;cur != null;cur = cur.next) {
        size++;
    }
    return size;
}

4. Introducir una cadena, la salida de la inversa de la K-ésima de nodo en la lista enlazada
(https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a? TPID = 13 && tqId = 11167 y rp = 2 & RU = / actividad / OJ & QRU = / TA / codificación de entrevistas / pregunta-clasificación)

public ListNode FindKthToTail(ListNode head,int k) {
    int length = size(head);
    if(head == null || k <= 0 || k > length) {
        return null;
    }
    int offset = length - k;
    ListNode cur = head;
    for(int i = 0;i < offset;i++) {
        cur = cur.next;
    }
    return cur;
}
public int size(ListNode head) {
    int size = 0;
    for(ListNode cur = head;cur != null;cur = cur.next) {
        size++;
    }
    return size;
}

5. Las dos listas ordenadas en una nueva lista está ordenada y vuelve
(https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null ) {
            return l2;
        }
        if(l2 == null) {
            return l1;
        }
        ListNode newlist = new ListNode(0);
        ListNode newtail = newlist;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                newtail.next = new ListNode(l1.val);
                l1 = l1.next;
                newtail = newtail.next;
            }else{
                newtail.next = new ListNode(l2.val);
                l2 = l2.next;
                newtail = newtail.next;
             }
        }
        if(l1 != null) {
            newtail.next = l1;
        }else{
            newtail.next = l2;
        }
         return newlist.next;
    }
}

Publicado 60 artículos originales · ganado elogios 23 · vistas 3324

Supongo que te gusta

Origin blog.csdn.net/weixin_44945537/article/details/102713704
Recomendado
Clasificación