数据结构与算法之链表训练营

1. 1 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList.(栈)

	public ArrayList<Integer> printListFromTailToHead(ListNode node) {
        //返回结果
        ArrayList<Integer> result = new ArrayList<>();
        //边界值处理
        if(node == null)
            return result;
        //辅助数据结构,分析题目要求,其过程与栈的后进先出十分相似
        Stack<Integer> stack = new Stack<>();
        //将链表中元素从头向尾依次入栈
        while(node != null){
            stack.push(node.val);
            node = node.next;
        }
        //将栈中的元素依次弹出并放入线性表中
        while(stack.size() != 0){
            result.add(stack.pop());
        }
        return result;
    }

1. 2 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList.(递归)

	public ArrayList<Integer> printListFromTailToHead(ListNode node) {
        //返回结果
        ArrayList<Integer> result = new ArrayList<>();
        //边界值处理
        if(node == null)
            return result;
        core(node,result);
        return result;
    }
    //递归函数实现栈的作用
    private void core(ListNode node,ArrayList<Integer> result){
        if(node.next != null){
            core(node.next,result);
        }
        result.add(node.val);
    }

2.1 输入一个链表,输出该链表中倒数第k个结点

方法一:快慢指针,时间复杂度O(n)
	public ListNode FindKthToTail(ListNode head,int k) {
        //输入参数合法性校验
        if(head == null || k < 1)
            return null;
        //快指针
        ListNode fast = head;
        //快指针先走k-1步
        while(k != 1 && fast.next != null){
            fast = fast.next;
            k--;
        }
        //k的值大于链表长度
        if(k != 1)
            return null;
        //快慢指针一起走
        while(fast.next != null){
            fast = fast.next;
            head = head.next;
        }
        return head;
    }
方法二:两趟处理,第一趟求出链表长度N,第二趟走N-k-1步即可
    public ListNode FindKthToTail(ListNode head,int k) {
        //输入参数合法性校验
       if(head == null || k < 1)
           return null;
       int len = 0;
       ListNode temp = head;
       //第一趟求链表长度
       while(temp != null){
           len++;
           temp = temp.next;
       }
       int diff = len - k;
       //第二趟找目标节点
       while(diff > 0){
           head = head.next;
           diff--;
       }
       //链表长度小于k
       if(diff != 0)
           return null;
       return head;
   }
方法三:利用栈,弹出的第k个即为倒数第k个节点(简单,略)

2.2 输入一个链表,反转链表

方法一:直接翻转
	public ListNode ReverseList(ListNode head) {
       //输入参数合法性校验
       if(head == null || head.next == null){
           return head;
       }
       //记录当前节点的前一个节点
       ListNode pre = null;
       //记录当前节点
       ListNode cur = head;
       //记录下一节点
       ListNode next = null;
       //反转链表,重新连接节点
       while(cur != null){
           next = cur.next;
           cur.next = pre;
           pre = cur;
           cur = next;
       }
       return pre;
   }
方法二:利用栈(简单、略)

2.3 输入两个单调递增的链表,输出两个链表合成后的链表,合成后的链表满足单调不减规则

 	public ListNode Merge(ListNode list1,ListNode list2) {
       //list1为null,则返回list2
       if(list1 == null)
           return list2;
       //list2为null,则返回list1
       if(list2 == null)
           return list1;
       // //list1和list2都不为null
       ListNode head = null;
       ListNode temp = null;
       while(list1 != null && list2 != null){
           if(list1.val < list2.val){
               if(head == null){
                   head = list1;
                   temp = list1;
               }else{
                   temp.next = list1;
                   temp = list1;
               }
               list1 = list1.next;
           }else{
               if(head == null){
                   head = list2;
                   temp = list2;
               }else{
                   temp.next = list2;
                   temp = list2;
               }
               list2 = list2.next;
           }
       }
       if(list1 != null){
           temp.next = list1;
       }else{
           temp.next = list2;
       }
       return head;
   }

2.3 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head

    public RandomListNode Clone(RandomListNode head){
           //输入参数合法性校验
           if(head == null)
               return null;
           //第一步,插入复制节点
           RandomListNode temp = head;
           while(temp != null){
               RandomListNode copy = new RandomListNode(temp.label);
               RandomListNode next = temp.next;
               temp.next = copy;
               copy.next = next;
               temp = next;
           }
           //第二步,复制随机指针
           temp = head;
           while(temp != null){
                RandomListNode random = temp.random;
                RandomListNode next = temp.next;
               if(random != null){
                   next.random = random.next;
               }
               temp = next.next;
           }
           //第三步,分离两个链表
           temp = head;
           RandomListNode newHead = temp.next;
           while(temp != null){
               RandomListNode next = temp.next;
               RandomListNode tempNext = next.next;
               temp.next = tempNext;
               if(tempNext == null){
                   next.next = tempNext;
                   break;
               }else{
                   next.next = tempNext.next;
               }
               temp = temp.next;
           }
           return newHead;
       }

2.4 输入两个链表,找出它们的第一个公共结点

    public ListNode FindFirstCommonNode(ListNode head1, ListNode head2) {
       if(head1 == null || head2 == null){
           return null;
       }
       //第一趟遍历求出两个链表的长度
       int len1 = 1;
       int len2 = 1;
       ListNode temp1 = head1;
       ListNode temp2 = head2;
       while(temp1.next != null){
           len1++;
           temp1 = temp1.next;
       }
       while(temp2.next != null){
           len2++;
           temp2 = temp2.next;
       }
       //判断链表最后一个节点是否相同
       if(temp1 != temp2){
           return null;
       }
       //第二趟找出第一个重合的节点
       int diff = len1 - len2;
       temp1 = head1;
       temp2 = head2;
       if(diff > 0){
           while(diff != 0){
               temp1 = temp1.next;
               diff--;
           }
           while(temp1 != null){
               if(temp1 == temp2){
                   return temp1;
               }
               temp1 = temp1.next;
               temp2 = temp2.next;
           }
       }else{
           while(diff != 0){
               temp2 = temp2.next;
               diff++;
           }
           while(temp1 != null){
               if(temp1 == temp2){
                   return temp1;
               }
               temp1 = temp1.next;
               temp2 = temp2.next;
           }
       }
       return null;
   } 

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

	public ListNode EntryNodeOfLoop(ListNode head){
       if(head == null || head.next == head)
           return head;
       //第一步,快慢指针判断是否存在环
       ListNode fast = head.next;
       ListNode slow = head;
       while(fast != slow && fast != null){
           fast = fast.next;
           slow = slow.next;
           if(fast != null){
               fast = fast.next;
           }else{
               return null;
           }
       }
       if(fast == null){
           return null;
       }
       //存在环,进一步处理获取环的长度
       fast = fast.next;
       int len = 1;
       while(fast != slow){
           len++;
           fast = fast.next;
       }
       //快慢指针找出入口节点
       fast = head;
       slow = head;
       while(len != 1){
           fast = fast.next;
           len--;
       }
       while(fast.next != slow){
           fast = fast.next;
           slow = slow.next;
       }
       return slow;
   }

2.6 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

方法一:使用辅助数据结构哈希表
    public ListNode deleteDuplication(ListNode head){
           if(head == null)
               return head;
           HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
           ListNode temp = head;
           //第一遍遍历记录重复元素
           while(temp != null){
               Integer count = map.get(temp.val);
               if(count == null){
                   map.put(temp.val,1);
               }else{
                   map.put(temp.val,count+1);
               }
               temp = temp.next;
           }
           //第二次遍历删除重复节点
           temp = head;
           ListNode result = null;
           ListNode cur = null;
           while(temp != null){
               Integer count =  map.get(temp.val);
               //如果当前节点为重复节点
               if(count > 1){
                   while(count != 0){
                       temp = temp.next;
                       count--;
                   }
               }else{
                   //当前节点不是重复节点
                   if(result == null){
                       result = temp;
                       cur = temp;
                   }else{
                       cur.next = temp;
                       cur = temp;
                   }
                   temp = temp.next;
               }
           }
           if(cur != null)
               cur.next = null;
           return result;
       }
使用递归处理
     public ListNode deleteDuplication(ListNode head){
           if(head == null || head.next == null){
               return head;
           }
           //当前节点为重复节点
           if(head.val == head.next.val){
               ListNode node = head.next;
               while(node != null && node.val == head.val){
                   node = node.next;
               }
               return deleteDuplication(node);
           }else{
               //当前节点不是重复节点
               head.next = deleteDuplication(head.next);
               return head;
           }
       }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值