LeetCode 链表题总结

     

最近花了几天时间,将链表题除带锁外的题目做完了,下面对链表题部分题目进行总结和分析。

链表题目解决方法

1、链表反转

2、快慢指针

链表反转模板

ListNode cur = head.next;
head.next = null;
while (cur != null) {
       ListNode next = cur.next;
       cur.next = head;
       head = cur;
       cur = next;
}

快慢指针模板

 

 

 

ListNode slow = head;
ListNode fast = head;
ListNode prev = slow;
while(fast != null && fast.next != null){
   prev =slow;
   slow = slow.next;
   fast = fast.next.next;
}

      遇到一些需要找到链表的中点问题时,可能会有链表长度为奇数或者偶数的情况,当长度为偶数时,模板里面 prev 为第一个中点,slow 为第二个中点,长度为奇数时 ,slow 为链表的中点。

部分题目分类

 1. 简单题

 1290 Convert Binary Number in a Linked List to Integer

 237 Delete Node in a Linked List

 21 Merge Two Sorted Lists

1669 Merge In Between Linked List

 

 2. 链表反转

 206 Reverse Linked List

 24 Swap Nodes in Pairs

 92 Reverse Linked List II

 25 Reverse Nodes in k-Group

 

 3. 快慢指针

 876  Middle of the Linked List

 141 Linked List Cycle

 142 Linked List Cycle II

 

posted @ 2020-12-12 18:08  lzyer  阅读(83)  评论(0编辑  收藏  举报