LeetCode & 剑指offer 经典题目总结——链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_25991865/article/details/88527985

1.重排链表

给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

解法:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        if(head==null || head.next==null){
            return;
        }
        //快慢指针找中间结点
        ListNode slow=head;
        ListNode fast=head;
        while(fast.next!=null && fast.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        //拆分链表
        ListNode after=slow.next;
        slow.next=null;
        //反转后半部分链表
        ListNode pre=null;
        while(after!=null){
            ListNode temp=after.next;
            after.next=pre;
            pre=after;
            after=temp;
        }
        //合并两个链表
        ListNode p1=head;
        ListNode p2=pre;
        while(p1!=null && p2!=null){
            ListNode p1temp=p1.next;
            ListNode p2temp=p2.next;
            p1.next=p2;
            p2.next=p1temp;
            p1=p1temp;
            p2=p2temp;
        }
        return;
    }
}

2.链表中的环入口结点

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

解法:

  1. 第一步,找环中相汇点。分别用slow,fast指向链表头部,slow每次走一步,fast每次走二步,直到slow==fast找到在环中的相汇点。
  2. 第二步,找环的入口。接上步,当fast==slow时,fast所经过节点数为2x,slow所经过节点数为x,设环中有n个节点,fast比slow多走k圈有2x=n*k+x; n=x;可以看出slow实际走了k个环的步数,再让fast指向链表头部,slow位置不变,slow,fast每次走一步直到fast == slow ,此时slow指向环的入口。
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null || head.next==null){
            return null;
        }
        ListNode slow=head;
        ListNode fast=head;
        while(fast.next!=null && fast.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
                fast=head;
                while(fast!=slow){
                    slow=slow.next;
                    fast=fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}

3.复制带随机指针的链表

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深拷贝。

解法:

  1. 复制每个节点,如:复制节点A得到A1,将A1插入节点A后面
  2. 遍历链表,A1->random = A->random->next
  3. 将链表拆分成原链表和复制后的链表
/**
 * Definition for singly-linked list with a random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if(head==null){
            return null;
        }
        //复制链表
        RandomListNode current=head;
        while(current!=null){
            RandomListNode temp=current.next;
            RandomListNode copy=new RandomListNode(current.label);
            current.next=copy;
            copy.next=temp;
            
            current=temp;
        }
        //2.
        current=head;
        while(current!=null){
            RandomListNode copy=current.next;
            if(current.random!=null){
                copy.random=current.random.next;
            }else{
                copy.random=null;
            }
            
            current=copy.next;
        }
        //拆分
        RandomListNode copyHead=head.next;
        current=head;
        while(current.next!=null){
            RandomListNode temp=current.next;
            current.next=temp.next;
            current=temp;
        }
        return copyHead;
    }
}

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

给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

解法:
链表查找中间点可以通过快慢指针来操作。找到中点后,要以中点的值建立一个树的根节点,然后需要把原链表断开,分为前后两个链表,都不能包含原中节点,然后再分别对这两个链表递归调用原函数,分别连上左右子节点即可。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; next = null; }
 * }
 */
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        return toBST(head, null);
    }
    
    private TreeNode toBST(ListNode head, ListNode tail) {
        if (head == tail){
            return null;
        }
        //找中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != tail && fast.next != tail) {
            fast = fast.next.next;
            slow = slow.next;
        }
        TreeNode root = new TreeNode(slow.val);
        root.left = toBST(head, slow);
        root.right = toBST(slow.next, tail);
        return root;
    }
}

5.反转从位置m到n的链表

请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL

解法:
不妨拿出四本书,摞成一摞(自上而下为 A B C D),要让这四本书的位置完全颠倒过来(即自上而下为 D C B A):
盯住书A,每次操作把A下面的那本书放到最上面
初始位置:自上而下为 A B C D
第一次操作后:自上而下为 B A C D
第二次操作后:自上而下为 C B A D
第三次操作后:自上而下为 D C B A

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        //建一个辅助节点,处理m=1的情况
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode preStart = dummy;
        ListNode start = head;
        for (int i = 1; i < m; i ++ ) {
            preStart = start;
            start = start.next;
        }
        // reverse
        for (int i = 0; i < n - m; i ++ ) {
            ListNode temp = start.next;
            start.next = temp.next;
            temp.next =preStart.next;
            preStart.next = temp;
        }
        return dummy.next;
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_25991865/article/details/88527985