链表题型思路错误总结

常见题目

206. 反转链表

在这里插入图片描述

关键点:定义前置指针。
在给cur.next复制前,需要定义好next节点防止断链。

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

    }

21. 合并两个有序链表

在这里插入图片描述
关键点:需要一个实质的头节点

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode pre = new ListNode();
        ListNode cur = pre;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        while (l1 != null) {
            cur.next = l1;
            l1 = l1.next;
            cur = cur.next;
        }
        while (l2 != null) {
            cur.next = l2;
            l2 = l2.next;
            cur = cur.next;
        }
        return pre.next;
    }
}

109. 有序链表转换二叉搜索树

在这里插入图片描述
关键点:如何对链表进行分割。

class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return new TreeNode(head.val);
        }
        return curser(head, null);
    }

    private TreeNode curser(ListNode head, ListNode tail) {
        if (head == tail) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != tail && fast.next != tail) {
            slow = slow.next;
            fast = fast.next.next;
        }
        TreeNode root = new TreeNode(slow.val);
        root.left = curser(head, slow);
        root.right = curser(slow.next, tail);
        return root;
    }

}

234. 回文链表

思路1:转换成数组,按照定义进行判断

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

思路2:找到中间节点,进行反转

为什么设置:ListNode slow = head; ListNode fast = head.next; 而不是ListNode slow = head; ListNode fast = head;

  • 找到需要反转链表的前一个节点,为了统一
    在这里插入图片描述
    在这里插入图片描述
    1 3 5 3 1是找到了饭互赞链表的前一个,但是1 2 2 1却找到了需要反转的当前这个。
    所以需要将fast往后移动一位,如下:
    在这里插入图片描述

在这里插入图片描述

class Solution {
    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        // 找到中间节点
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast !=null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        // 反转后半部分
        ListNode second = slow.next;
        slow.next = null;
        second = reverse(second);
        while(second != null) {
            if (head.val != second.val){
                return false;
            }
            second = second.next;
            head = head.next;
        }
        return true;

    }

    private ListNode reverse(ListNode head){
        if(head == null ||head.next == null){
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    } 
}

876. 链表的中间结点

思路:如上题解题思路

class Solution {
    public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        // 找到中间节点
        ListNode slow = head;
        ListNode fast = head;
        while(fast !=null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

19. 删除链表的倒数第 N 个结点

在这里插入图片描述

  • 为什么新加一个头结点
  • 为了防止删除第一节点,例如n = 5时,不好操作,加一个头结点有些解决这类问题。
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null){
            return head;
        }
        ListNode pre = new ListNode();
        pre.next = head;
        ListNode cur = pre;
        ListNode fast = head;
        for (int i = 0; i < n; i++) {
            if (fast != null) {
                fast = fast.next;
            }
        }
        while(fast != null) {
            cur = cur.next;
            fast = fast.next;
        }

        cur.next = cur.next.next;
        return pre.next;
    }
}

86. 分隔链表

在这里插入图片描述

class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null) {
            return head;
        }
        ListNode l1 = new ListNode();
        ListNode l2 = new ListNode();
        ListNode cur = head;
        ListNode small = l1;
        ListNode big = l2;
        while(cur != null) {
            ListNode next = cur.next;
            if (cur.val < x) {
                small.next = cur;
                small = small.next;
            } else {
                big.next = cur;
                big = big.next;
            }
             cur.next = null;
             cur = next;
        }
        small.next = l2.next;
        return l1.next;
        
    }
}

92. 反转链表 II

在这里插入图片描述

  • left需要指向移动前一个,right移动到需要反转的位置。
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode pre = new ListNode();
        pre.next = head;
        ListNode le = pre;
        ListNode rt = pre;
        
        for (int i = 0; i < left - 1; i++){
            le = le.next;
        }
        ListNode l1 = null;
        if(le != null) {
            l1 = le.next;
        }
        for (int i = 0; i < right; i++){
            rt = rt.next;
        }
        ListNode l2 = null;
        if(rt != null) {
            l2 = rt.next;
            rt.next = null;
        }
        le.next = reverseListNode(l1);
        l1.next = l2;
        return pre.next;
        

    }

    private ListNode reverseListNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

143. 重排链表

  • 思路:
      1. 找到中间节点
      1. 反转中间节点后的顺序
      1. 合并节点

第一步:找到中间节点
在这里插入图片描述

    private ListNode getMid(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
  • 第二步:反转链表
    private ListNode reverse(ListNode head){
        if(head == null || head.next == null) {
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;

    }
  • 第三步:合并链表
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        if (head == null || head.next == null) {
            return;
        }
        ListNode mid = getMid(head);
        ListNode l1 = head;
        ListNode temp = mid.next;
        mid.next = null;
        ListNode l2 = reverse(temp);
        ListNode pre = new ListNode();
        ListNode  cur = pre;
        while(l1 != null && l2 != null) {
            cur.next = l1;
            l1 = l1.next;
            cur = cur.next;
            cur.next = l2;
            l2 = l2.next;
            cur = cur.next;
        }
        while(l1 != null) {
            cur.next = l1;
            l1 = l1.next;
            cur = cur.next;
        }
        while (l2 != null) {
            cur.next = l2;
            l2 = l2.next;
            cur = cur.next;
        }
        head = pre.next;
    }
}

从尾到头打印链表

删除链表的节点

链表中倒数第K个节点

反转链表

合并两个排序的链表

复杂两个链表的复制

两个链表的第一个公共节点

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表是一种常见的数据结构,它可以在链表中的每个节点中保存指向前一个节点和后一个节点的引用。这使得在双向链表中,我们可以在 O(1) 的时间复杂度内访问前一个节点和后一个节点。 实现双向链表思路如下: 1. 定义节点类:首先,我们需要定义一个节点类作为双向链表的基本单位。节点类应该包含两个重要的属性:数据和指针。数据用于存储节点中的值,指针用于指向前一个节点和后一个节点。 ```python class Node: def __init__(self, data): self.data = data self.prev = None self.next = None ``` 2. 构建双向链表类:接下来,我们需要构建一个双向链表类来管理整个链表。该类应该包含头节点和尾节点的引用。 ```python class DoublyLinkedList: def __init__(self): self.head = None self.tail = None ``` 3. 实现插入操作:要在双向链表中插入一个新节点,我们需要考虑两种情况:插入在链表的开头和插入在链表的其他位置。 - 插入在开头:如果要插入一个新节点作为链表的新头部,我们只需要将新节点的 next 指针指向当前头部节点,同时将当前头部节点的 prev 指针指向新节点。最后,更新链表的头部引用即可。 - 插入在其他位置:如果要插入一个新节点在链表的其他位置,我们需要先找到插入位置的前一个节点。然后,将新节点的 prev 指针指向前一个节点,将新节点的 next 指针指向前一个节点的下一个节点,再将前一个节点的 next 指针指向新节点,最后将新节点的下一个节点的 prev 指针指向新节点。 ```python def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node self.tail = new_node else: new_node.next = self.head self.head.prev = new_node self.head = new_node ``` 4. 实现删除操作:要在双向链表中删除一个节点,我们需要考虑几种情况:删除头节点、删除尾节点以及删除其他位置的节点。 - 删除头节点:如果要删除头节点,我们只需要将头节点的下一个节点作为新的头节点,并将新头节点的 prev 指针设为 None。 - 删除尾节点:如果要删除尾节点,我们只需要将尾节点的前一个节点作为新的尾节点,并将新尾节点的 next 指针设为 None。 - 删除其他位置的节点:如果要删除其他位置的节点,我们需要先找到要删除的节点。然后,将要删除节点的前一个节点的 next 指针指向要删除节点的下一个节点,将要删除节点的下一个节点的 prev 指针指向要删除节点的前一个节点。 ```python def delete(self, data): if self.head is None: return current = self.head while current: if current.data == data: if current.prev: current.prev.next = current.next else: self.head = current.next if current.next: current.next.prev = current.prev else: self.tail = current.prev break current = current.next ``` 这样,双向链表的基本实现就完成了。当然,还可以根据需要添加其他操作,比如查找节点、获取链表长度等。请注意,在操作双向链表时,我们需要特别注意处理边界情况,比如链表为空或只有一个节点的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值