链表问题

111 阅读2分钟
  • 题目 1.输入链表的头节点,奇数长度返回中点,偶数长度返回上中点

  • coding

public static ListNode midUp(ListNode head){
    if (head == null || head.next == null || head.next.next == null){
        return head;
    }
    ListNode slow = head.next;
    ListNode fast = head.next.next;
    while(fast.next!=null && fast.next.next != null){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
  • 题目 2.输入链表的头节点,奇数长度返回中点,偶数长度返回下中点

  • coding

public static ListNode midDown(ListNode head){
    if (head == null || head.next == null){
        return head;
    }
    if (head.next.next == null){
        return head.next;
    }
    ListNode slow = head.next;
    ListNode fast = head.next;
    while(fast.next !=null && fast.next.next!=null){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
  • 题目 3.输入链表的头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个

  • coding

public static Node midOrUpMidPreNode(ListNode head){
    if (head == null || head.next ==null || head.next.next==null){
        return null;
    }
    ListNode slow = head;
    ListNode fast = head.next.next;
    while(fast.next!=null && fast.next.next != null){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
  • 题目 4.输入链表的头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个
public static Node midOrDownMidPreNode(ListNode head){
    if (head == null ||head.next == null){
        return null;
    }
    if (head.next.next == null){
        return head;
    }
    ListNode slow = head;
    NListNodeode fast = head.next;
    while(fast.next!=null && fast.next.next!=null){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
  • 题目 5.反转一个单链表

  • coding

public static ListNode singleReverseLinkedList(ListNode head){
    if (head == null || head.next == null){
        return head;
    }

    ListNode pre = null;
    ListNode next = null;

    while(head!=null){
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
}
  • 题目 6.反转一个双向链表
  • coding
public static class DoubleNode{
    public int value;
    public DoubleNode next;
    public DoubleNode last;
    public DoubleNode() {}
    public DoubleNode(int value) {
        this.value = value;
    }
}

public static DoubleNode reverseDoubleLinkedList(DoubleNode head){
    if (head == null || head.next == null){
        return null;
    }
    DoubleNode next = null;
    DoubleNode pre = null;
    while(head!=null){
        next = head.next;
        head.next = pre;
        head.last = next;
        pre = head;
        head = next;
    }
    return pre;
}
  • 题目 7.判断是否是回文链表

  • M1 :使用栈将链表的数据一个一个压入进去,最后一个一个的弹出来,看和链表的元素是否是一致的,如果都是一致的那么就是回文,如果有一个不一致,就不是回文结构

public static boolean isPalindorm1(Node head){
    if (head==null){
        return true;
    }
    Stack<Node> stack = new Stack<>();
    Node cur = head;

    while(cur!=null){
        stack.push(cur);
        cur = cur.next;
    }
    cur = head;
    while(!stack.isEmpty()){
        Node node = stack.pop();
        if (cur.value!=node.value){
            return false;
        }
        cur = cur.next;
    }
    return true;
}
  • M2 :同样也是使用栈,只不过这次使用栈结构只是将链表结构一半的数据压入进去即可,然后从栈中弹出依次判断
public static boolean isPalindorm2(Node head){
    if (head==null || head.next == null){
        return true;
    }
    Node right = head.next;
    Node cur = head;
    while(cur.next!=null && cur.next.next!=null){
        right = right.next;
        cur = cur.next.next;
    }
    Stack<Integer> stack = new Stack<>();
    while(right!=null){
        stack.push(right.value);
        right = right.next;
    }
    while(!stack.isEmpty()){
        Integer pop = stack.pop();
        if (pop!= head.value){
            return false;
        }
    }
    return true;
}
  • M3 使用几个有限的变量
  • coding
public static boolean isPalindorm3(Node head){
    if (head == null || head.next == null){
        return true;
    }
    boolean res = true;
    Node n1 = head;
    Node n2 = head;
    while(n2.next!=null && n2.next.next != null){
        n1 = n1.next;
        n2 = n2.next.next;
    }
    n2 = n1.next;
    n1.next = null;
    Node n3 = null;
    while(n2!=null){
        n3 = n2.next;
        n2.next = n1;
        n1 = n2;
        n2 = n3;
    }
    n3 = n1;
    n2 = head;  //回到头部
    while(n1 !=null && n2!=null){
        if (n1.value!= n2.value){
            res = false;
            break;
        }
        n1 = n1.next;
        n2 = n2.next;
    }
    n1 = n3.next;
    n3.next = null;
    while(n1!=null){
        n2 = n1.next;
        n1.next = n3;
        n3 = n1;
        n1 = n2;
    }
    return res;
}

题目8: 将一个链表划分成左边小,中间相等,右边大的形式

  • coding
public static Node listPartition2(Node head,int voite){
    Node sh = null , st = null;
    Node eh = null , et = null;
    Node bh = null , bt = null;
    Node next = null;
    while(head!=null){
        next = head.next;
        head.next = null;
        if (head.value > voite){
            if (bh == null){
                bh = head;
                bt = head;
            }else{
                bt.next = head;
                bt=head;
            }
        }else if (head.value == voite){
            if (eh == null){
                eh = head;
                et = head;
            }else{
                et.next = head;
                et = head;
            }
        }else{
            if (sh == null){
                sh = head;
                st = head;
            }else{
                sh.next = head;
                st = head;
            }
        }
        head = next;
    }
    //边界
    if (st!=null){
        st.next = eh;
        et = eh==null?st:et;
    }
    if (et!=null){
        et.next = bh;
    }
    return sh !=null?sh:eh!=null?eh:bh;
}

题目9 : 合并两个有序的单链表

  • 找小头

  • coding

public static ListNode mergerList2(ListNode head1,ListNode head2){
    if (head1 == null && head2 == null){
        return null;
    }
    if (head1!=null && head2 == null){
        return head1;
    }
    if (head1==null && head2 != null){
        return head2;
    }

    //找小头
    ListNode head = head1.value <= head2.value?head1:head2;

    ListNode cur1 = head.next;
    ListNode cur2 = head == head1?head2:head1;

    ListNode pre = head;
    while(cur1!=null && cur2!=null){
        if (cur1.value <= cur2.value){
            pre.next = cur1;
            cur1 = cur1.next;
        }else{
            pre.next = cur2;
            cur2 = cur2.next;
        }
        pre = pre.next;
    }
    pre.next = cur1!=null?cur1:cur2;
    return head;
}