LeetCode —— 链表

持续更新中........... 

定义链表

public class ListNode {
    int val;    //节点的值

    ListNode next;  //下一个节点

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

203. 移除链表元素

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

示例1:输入:head = [1,2,6,3,4,5,6], val = 6        输出:[1,2,3,4,5]

示例2:输入:head = [], val = 1        输出:[]

示例3:输入:head = [7,7,7,7], val = 7        输出:[]

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode pre = new ListNode();
        pre.next = head;
        ListNode cur = pre;
        while(cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return pre.next;
    }
}

707. 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 nextval 是当前节点的值,next 是指向下一个节点的指针。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
class MyLinkedList {
    int size;
    ListNode head;

    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }

    public int get(int index) {
        if(index < 0 || index >= size){
            return -1;
        }
        ListNode currentNode = head;
        for(int i = 0; i <= index; i++){
            currentNode = currentNode.next;
        }
        return currentNode.val;
    }

    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    public void addAtIndex(int index, int val) {
        if(index > size){
            return;
        }
        if(index < 0){
            index = 0;
        }
        size++;
        ListNode pred = head;
        for(int i = 0; i < index; i++){
            pred = pred.next;
        }
        ListNode addNode = new ListNode(val);
        addNode.next = pred.next;
        pred.next = addNode;
    }

    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }
        size--;
        ListNode pred = head;
        for(int i = 0; i < index; i++){
            pred = pred.next;
        }
        pred.next = pred.next.next;
    }
}

206. 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例1:输入:head = [1,2,3,4,5]        输出:[5,4,3,2,1]

示例2:输入:head = []        输出:[]

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode temp = null;
        while(cur != null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

24. 两两交换链表中的节点

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例1:输入:head = [1,2,3,4]        输出:[2,1,4,3]

示例2:输入:head = []        输出:[]

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode virtualNode = new ListNode();
        virtualNode.next = head;
        ListNode pre = virtualNode;

        while (head != null && head.next != null) {
            ListNode temp = head.next.next; // 缓存

            //交换
            pre.next = head.next;
            head.next.next = head;

            //交换完连接上剩余的节点
            head.next = temp;

            //继续下一次循环
            pre = head;
            head = temp;
        }
        return virtualNode.next;
    }
}

21. 合并两个有序链表

 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例:输入:l1 = [1,2,4], l2 = [1,3,4]         输出:[1,1,2,3,4,4]

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode pre = new ListNode();

        ListNode cur = pre;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }

        cur.next = list1 == null ? list2 : list1;
        return pre.next;
    }
}
//递归
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null){
            return list2;
        }else if(list2 == null){
            return list1;
        }else if(list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        }else{
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }
}

19. 删除链表的倒数第n个节点

 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

示例1:输入:head = [1,2,3,4,5], n = 2        输出:[1,2,3,5]

示例1:输入:head = [1], n = 1         输出:[]

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode virtualNode = new ListNode();
        virtualNode.next = head;

        ListNode fastIndex = virtualNode;
        ListNode slowIndex= virtualNode;

        for(int i = 0; i < n; i++){
            fastIndex = fastIndex.next;
        }

        while(fastIndex.next != null) {
            fastIndex = fastIndex.next;
            slowIndex = slowIndex.next;
        }

        slowIndex.next = slowIndex.next.next;
        return virtualNode.next;
    }
}

160.相交链表

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。 

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode A = headA;
        ListNode B = headB;
        while(A != B){
            A = A != null ? A.next : headB;
            B = B != null ? B.next : headA;
        }
        return A;
    }
}

141. 环形链表

给你一个链表的头节点 head ,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){    //说明有环
                return true;
            }
        }
        return false;
    }
}

142. 环形链表II

给定一个链表的头节点  head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。不允许修改 链表。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 

class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                slow = head;
                while(slow != fast){
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}

234. 回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

class Solution {
    public boolean isPalindrome(ListNode head) {
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        cur = head;
        
        int[] array = new int[len];
        for(int i = 0; i < len; i++){
            array[i] = cur.val;
            cur = cur.next;
        }
        
        int left = 0;
        int right = len - 1;
        while(left < right){
            if(array[left] != array[right]){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

143. 重排链表

给定一个单链表 L 的头节点 head ,单链表 L 表示为:L0 → L1 → … → Ln - 1 → Ln;请将其重新排列后变为:L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …;不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

class Solution {
    public void reorderList(ListNode head) {
        List<ListNode> list = new ArrayList<>();
        while(head != null){
            list.add(head);
            head = head.next;
        }

        head = list.get(0);
        int left = 1;
        int right = list.size() - 1;
        for(int i = 1; i < list.size(); i++){
            if(i % 2 != 0){
                head.next = list.get(right);
                right--;
            }else{
                head.next = list.get(left);
                left++;
            }
            head = head.next;
        }
        head.next = null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值