链表经典示例

链表反转

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        
        
        ArrayList<Integer> arr = new ArrayList<>();
        if(listNode == null){
            return arr;
        }

        ListNode pre = listNode;
        ListNode rear = pre.next;
        pre.next = null;
        while(rear != null){
            ListNode tmp = rear.next;
            rear.next = pre;
            pre = rear;
            rear = tmp;
        }
        
        while(pre != null){
            arr.add(pre.val);
            pre = pre.next;
        }
        return arr;
    }
}

倒数第k个节点

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        
        if(head == null || k == 0){
            return null;
        }
        ListNode first = head;
        ListNode second = head;
        for(int i = 0; i < k - 1 && second != null; ++i){
            second = second.next;
        }
        
        if(second == null){
            return null;
        }
        
        while(second.next != null){
            first = first.next;
            second = second.next;
        }
        
        return first;

    }
}

链表合并

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }

        ListNode l1 = list1;
        ListNode l2 = list2;
        ListNode head = null;
        ListNode last = null;
        while(l1 != null && l2 != null){
            
            if(l1.val <= l2.val){
                
                if(last == null){
                    head = l1;
                    last = l1;
                }else{
                    last.next = l1;
                    last = l1;
                }
                l1 = l1.next;
                
            }else{
                
                if(last == null){
                    head = l2;
                    last = l2;
                }else{
                    last.next = l2;
                    last = l2;
                }
                
                l2 = l2.next;
            }

        }
        if(l1 != null){
            last.next = l1;
        }
        if(l2 != null){
            last.next = l2;
        }
        
        return head;

    }
}

链表复制

         public static RandomListNode Clone(RandomListNode pHead) {

        if (pHead == null) {
            return null;
        }

        RandomListNode first = pHead;

        while (first != null) {
            RandomListNode tmp = first.next;
            RandomListNode newNode = new RandomListNode(first.label);
            first.next = newNode;
            newNode.next = tmp;
            first = tmp;
        }

        RandomListNode two = pHead;

        while (two != null) {

            if(two.random != null){
                two.next.random = two.random.next;
            }
            two = two.next.next;

        }

        RandomListNode cur = pHead;
        RandomListNode result = pHead.next;
        
        while(cur.next != null){
            
            RandomListNode tmp = cur.next;
            cur.next = tmp.next;
            cur = tmp;
            
        }
        return result;
    }

两个链表的公共节点

    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
         
        if(pHead1 == null || pHead2 == null){
            return null;
        }
        
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        int length1 = 0;
        int length2 = 0;
        while(cur1 != null){
            length1++;
            cur1 = cur1.next;
        }
        while(cur2 != null){
            length2++;
            cur2 = cur2.next;
        }
        cur1 = pHead1;
        cur2 = pHead2;
        if(length2 >= length1){
            for(int i = 0; i < length2-length1; ++i){
                cur2 = cur2.next;
            }
        }else{
            for(int i = 0; i < length1-length2; ++i){
                cur1 = cur1.next;
            }
        }
        
        while(cur1 != null){
            
            if(cur1 == cur2){
                return cur1;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }

判断链表有环

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if(pHead == null){
            return null;
        }
        
        ListNode fast = pHead;
        ListNode slow = pHead;
        
        do{
           fast = fast.next;
           if(fast == null){
               return null;
           }
            fast = fast.next;
            slow = slow.next;
            
        }while(fast != slow);
        
        fast = pHead;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;

    }

删除链表重复节点

    public ListNode deleteDuplication(ListNode pHead)
    {
        
         if(pHead == null){
            return null;
        }
        ListNode head = new ListNode(-1);
        head.next = pHead;
        ListNode pre = head;
        ListNode next1 = pHead;
        ListNode next2 = pHead.next;

        while(next2 != null){

            if(next2.val == next1.val){

                while(next2 != null && next2.val == next1.val){
                    next2 = next2.next;
                    next1 = next1.next;

                }
                
                if(next2 == null){
                    pre.next = next2;
                    break;
                    
                }else{
                    pre.next = next2;
                    next1 = next2;
                    next2 = next2.next;
                }
                

            }else{
                pre = next1;
                next1 = next2;
                next2 = next2.next;
            }

        }

        return head.next;

    }

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值