数据结构与算法-链表专题

本文深入解析了链表的各种操作,包括反转链表、部分反转、旋转链表以及合并两个有序链表。提供了详细的算法思路和代码实现,帮助读者理解和掌握链表的基本操作和常见面试题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.反转链表 

206-Reverse a singly linked list.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null)
            return head;
        ListNode pre = null;
        ListNode cur = head;
        ListNode behind = cur.next;
        
        while(cur!=null){
            cur.next = pre;
            pre = cur;
            if(behind==null)
                break;
            cur = behind;
            behind = behind.next;
        }
        return cur;
    }
}

92-Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

class Solution(object):
    def reverseBetween(self,head,m,n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        if m == n:
            return head
        
        dummyHead = ListNode(0)
        dummyHead.next = head
        pre = dummyHead
        
        #pre指针移到m点前面
        for i in range(m-1):
            pre = pre.next
        
        #reverse the [m, n] nodes
        reverse = None
        cur = pre.next #从m节点开始
        #从m到n的节点数
        for i in range(n - m + 1):
            temp = cur.next
            cur.next = reverse
            reverse = cur
            cur = temp
        '''
        注意两个节点
        一个是m前的一个节点--->pre
        一个是n后面的一个节点--->cur
        '''
        pre.next.next = cur
        pre.next = reverse
        
        return dummyHead.next
        

 

leetcode-61-Rotate List

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

解决思路:先用一个指针找到链表的长度i,然后找到反转后的尾节点i-n%i

class Solution {
    public ListNode rotateRight(ListNode head, int n) {
        if (head==null||head.next==null) 
            return head;
        ListNode dummy=new ListNode(0);
        dummy.next=head;
        ListNode fast=dummy,slow=dummy;

        int i;
        for (i=0;fast.next!=null;i++)//Get the total length 
            fast=fast.next;

        for (int j=i-n%i;j>0;j--) //Get the i-n%i th node
            slow=slow.next;

        fast.next=dummy.next; //Do the rotation
        dummy.next=slow.next;
        slow.next=null;

        return dummy.next;
    }
}

 LeetCode-21-链表-合并两个有序链表

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

 方案一:归并中的merge方法

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

class Solution {
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        
        //定义新链表头
        ListNode temp = new ListNode(0);

        //定义三个链表的移动指针
        ListNode current1=l1;
        ListNode current2=l2;
        ListNode current3=temp;

        int t=0;

        while(current1 !=null && current2 !=null){
            if(current1.val < current2.val){
                current3.next=current1;
                current3 = current3.next;
                current1=current1.next;
             
            }else{

                current3.next=current2;
                current3 = current3.next;
                current2=current2.next;
                
            }
        }
         
        
        //l2排完,l1未排完  
        while(current1 !=null){
                
                current3.next=current1;
                current3 = current3.next;
                current1=current1.next;
                  
        }
            
        while(current2 !=null){

                current3.next=current2;
                current3 = current3.next;
                current2=current2.next;
        }
        
        return temp.next;
            
        
    }
}

 方案二:递归

        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
                if(l1==null){
                    return l2;
                }
                if(l2==null){
                    return l1;
                }
                ListNode mergeNode;
                if(l1.val<l2.val){
                    mergeNode = l1;
                    mergeNode.next = mergeTwoLists(l1.next,l2);
                }else{
                    mergeNode = l2;
                    mergeNode.next = mergeTwoLists(l1,l2.next);
                }
                return mergeNode;        
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值