链表经典算法题实现
2016-09-18 16:29
176 查看
本文包含链表的以下内容:
1、单链表的创建和遍历
2、求单链表中节点的个数
3、查找单链表中的倒数第k个结点(剑指offer,题15)
4、查找单链表中的中间结点
5、合并两个有序的单链表,合并之后的链表依然有序【出现频率高】(剑指offer,题17)
6、单链表的反转【出现频率最高】(剑指offer,题16)
7、从尾到头打印单链表(剑指offer,题5)
8、判断单链表是否有环
9、取出有环链表中,环的长度
10、单链表中,取出环的起始点(剑指offer,题56)。本题需利用上面的第8题和第9题。
11、判断两个单链表相交的第一个交点(剑指offer,题37)
1、单链表的创建和遍历
2、求单链表中节点的个数
3、查找单链表中的倒数第k个结点(剑指offer,题15)
4、查找单链表中的中间结点
5、合并两个有序的单链表,合并之后的链表依然有序【出现频率高】(剑指offer,题17)
6、单链表的反转【出现频率最高】(剑指offer,题16)
7、从尾到头打印单链表(剑指offer,题5)
8、判断单链表是否有环
9、取出有环链表中,环的长度
10、单链表中,取出环的起始点(剑指offer,题56)。本题需利用上面的第8题和第9题。
11、判断两个单链表相交的第一个交点(剑指offer,题37)
package com.he; import java.util.Stack; import com.he.LinkList.Node; public class test32 { class Node{ int data; Node next; Node(int data){ this.data = data; } } public Node head =null; public Node current = null; //添加节点 public void add(int data){ if(head==null){ head = new Node(data); current = head; }else{ current.next = new Node(data); current = current.next; } } //方法重载:向链表中添加结点 public void add(Node node){ if(head==null){ head = node; current = node; }else{ current.next = node; current = current.next; } } //遍历节点 public void print(Node node){ current = node; while(current!=null){ System.out.println(current.data); current = current.next; } } //求单链表节点 public int getLength(Node node){ current = node; int length = 0; if(current == null){ return 0; } while(current!=null){ length++; current = current.next; } return length; } //查找单链表中的倒数第k个结点 public Node findLastNode(Node node,int k){ if(node==null || k==0){ return null; } Node first = node; Node second = node; for (int i = 0; i < k-1; i++) { second = second.next; if(second == null){ return null; } } while(second.next!=null){ first = first.next; second = second.next; } return first; } //查找单链表中的中间结点 public Node findMiddle(Node node){ if(node==null){ return null; } Node first = node; Node second = node; while(first!=null && second!=null){ if(second.next!=null){ second = second.next.next; }else{ return first; } first = first.next; } return first; } //合并两个有序的单链表,合并之后的链表依然有序 public Node getMergeLinkList(Node head1,Node head2){ if(head1==null && head2==null){ return null; } if(head1==null){ return head2; } if(head2==null){ return head1; } Node head; Node current; if(head1.data < head2.data){ head = head1; current = head1; head1 = head1.next; }else{ head = head2; current = head2; head2 = head2.next; } while(head1!= null && head2!= null){ if(head1.data < head2.data){ current.next = head1; current = current.next; head1 = head1.next; }else{ current.next = head2; current = current.next; head2 = head2.next; } } if(head1 == null){ current.next = head2; } if(head2 == null){ current.next = head1; } return head; } //单链表的反转 public Node reverseList(Node head){ if(head==null && head.next==null){ return head; } Node reverseHead = null; Node next = null; Node current = head; while(current!=null){ next = current.next; current.next = reverseHead; reverseHead = current; current = next; } return reverseHead; } //从尾到头打印单链表 public void reversePrint(Node head){ if(head == null){ return; } Stack<Node> stack = new Stack<Node>(); Node current = head; while(current!=null){ stack.push(current); current = current.next; } while(stack.size()>0){ System.out.println(stack.pop().data); } } //判断单链表是否有环 public boolean hasCycle(Node head){ if(head == null){ return false; } Node first = head; Node second = head; while(second!=null){ first = first.next; if(second.next!=null){ second = second.next.next; }else{ return false; } if(first == second){ return true; } } return false; } //取出有环链表中,环的长度 public int getCycleLength(Node node){ int length = 0; Node current = node; while(current!=null){ current = current.next; length++; if(current == node){ return length; } } return length; } //单链表中,取出环的起始点 public Node getCycleStart(Node head,int cycleLength){ if(head==null){ return null; } Node first = head; Node second = head; for (int i = 0; i < cycleLength; i++) { second = second.next; } while(first!=null&& second!=null){ if(first == second){ return first; } first = first.next; second = second.next; } return null; } //求两个单链表相交的第一个交点 public Node getFirstCommonNode(Node head1,Node head2){ if(head1==null|| head2==null){ return null; } int length1 = getLength(head1); int length2 = getLength(head2); int lengthDif = 0; Node longHead = null; if(length1 > length2){ lengthDif = length1-length2; longHead = head1; }else{ lengthDif = length2-length1; longHead = head2; } for (int i = 0; i < lengthDif; i++) { longHead = longHead.next; } while(longHead!=null && head2!=null){ longHead = longHead.next; head2 = head2.next; if(longHead == head2){ return head2; } } return null; } public static void main(String[] args) { test32 sh = new test32(); for (int i = 0; i < 4; i++) { sh.add(i); } // sh.print(sh.head); // System.out.println(sh.getLength(sh.head)); // System.out.println(sh.findLastNode(sh.head, 3).data); //sh.print(sh.reverseList(sh.head)); sh.add(sh.head); System.out.println(sh.hasCycle(sh.head)); System.out.println(sh.hasCycle(sh.head)); System.out.println(sh.getCycleLength(sh.head)); System.out.println(sh.getCycleStart(sh.head, sh.getCycleLength(sh.head)).data); // test32 sh1 = new test32(); // for (int i = 6; i < 8; i++) { // sh1.add(i); // } // sh.print(sh.getMergeLinkList(sh.head, sh1.head)); } }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- 【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- 【LeetCode-面试算法经典-Java实现】【092-Reverse Linked List II(反转单链表II)】
- 经典算法与数据结构的c++实现——带头结点的单链表
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- 【LeetCode-面试算法经典-Java实现】【023-Merge k Sorted Lists(合并k个排好的的单链表)】
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- 经典算法学习——链表实现冒泡排序
- 【LeetCode-面试算法经典-Java实现】【143-Copy List with Random Pointer(有随机指针的链表复制)】
- 【LeetCode-面试算法经典-Java实现】【141-Linked List Cycle(单链表中有环)】
- 【LeetCode-面试算法经典-Java实现】【061-Rotate List(旋转单链表)】
- 【LeetCode-面试算法经典-Java实现】【025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)】
- 【LeetCode-面试算法经典-Java实现】【021-Merge Two Sorted Lists(合并两个排好序的单链表)】
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- java链表实现约瑟夫算法
- Sql语句排序的算法 是不是应用经典算法冒泡法实现的?
- 基于GPU实现的经典光照模型算法:phong光照模型
- 数据结构经典算法汇总___图的邻接表实现