登录
  • #刷题
  • #careercup

很简单的链表找环

TonyJang
1150
2
进入debug的时候有bug,实在找不到,求助大家

1.Node.java
package Chapter2;[br][/br][br][/br] [br][/br][br][/br]public class Node<T> {[br][/br][br][/br]public T data;[br][/br][br][/br]public Node<T> next;[br][/br][br][/br]public Node(T d, Node<T> n){[br][/br][br][/br]data = d;[br][/br][br][/br]next = n;[br][/br][br][/br]}[br][/br][br][/br]}
2.MylinkedList.java
package Chapter2;[br][/br][br][/br] [br][/br][br][/br]//Singly Linked List[br][/br][br][/br]public class MyLinkedList<T> {[br][/br][br][/br]public Node<T> head;[br][/br][br][/br] [br][/br][br][/br]public MyLinkedList(Node<T> h) {[br][/br][br][/br]head = h;[br][/br][br][/br]}[br][/br][br][/br]public MyLinkedList() {[br][/br][br][/br]head = null;[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public MyLinkedList(T[] dataArray) {[br][br][/br]if (dataArray == null || dataArray.length <= 0)[br][/br][br][/br]return;[br][/br][br][/br]head = new Node<>(dataArray[0], null);[br][/br][br][/br]Node<T> node = head;[br][/br][br][/br]for (int i = 1; i < dataArray.length; i++) {[br][/br][br][/br]node.next = new Node<T>(dataArray[i], null);[br][/br][br][/br]node = node.next;[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public void print() {[br][/br][br][/br]Node<T> cur = head;[br][/br][br][/br]while (cur != null) {[br][/br][br][/br]System.out.print(cur.data);[br][/br][br][/br]if (cur.next != null) {[br][/br][br][/br]System.out.print(" -> ");[br][/br][br][/br]}[br][/br][br][/br]cur = cur.next;[br][/br][br][/br]}[br][/br][br][/br]System.out.println();[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public void prepend(T data) {[br][/br][br][/br]Node<T> next = head != null ? head.next : null;[br][/br][br][/br]head = new Node<T>(data, next);[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public MyLinkedList<T> reverse() {[br][/br][br][/br]if(head == null) return null;[br][/br][br][/br]Node<T> newHead = head;[br][/br][br][/br]Node<T> cur = head.next;[br][/br][br][/br]head.next = null;[br][/br][br][/br]while(cur != null) {[br][/br][br][/br]Node<T> next = cur.next;[br][/br][br][/br]cur.next = newHead;[br][/br][br][/br]newHead = cur;[br][/br][br][/br]cur = next;[br][/br][br][/br]}[br][/br][br][/br]head = newHead;[br][/br][br][/br]return this;[br][/br][br][/br]}[br][/br][br][/br]}3.findbegning.java[code]package Chapter2;[br][/br][br][/br]public class findBeginning{[br][/br][br][/br]public static<T> Node<T> findBegining(MyLinkedList<T> list){[br][/br][br][/br]if(list.head==null||list==null)[br][/br][br][/br]return null;[br][/br][br][/br]Node<T> fast=list.head;[br][/br][br][/br]Node<T> slow=list.head;[br][/br][br][/br]while(fast!=null&&fast.next!=null){[br][/br][br][/br]slow=slow.next;[br][/br][br][/br]fast=fast.next.next;[br][/br][br][/br]if(slow==fast) {break;[br][/br][br][/br]}[br][/br][br][/br]if(fast ==null|| fast.next==null){[br][/br][br][/br]return null ;[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br]slow=list.head;[br][/br][br][/br]while(slow!=fast){[br][/br][br][/br]slow=slow.next;[br][/br][br][/br]fast=fast.next;[br][/br][br][/br]}[br][/br][br][/br]return fast;[br][/br][br][/br]}[br][/br][br][/br]public static void main(String[] args){[br][br][/br]MyLinkedList<Integer> list=new MyLinkedList<>(new Integer[] {1,2,3,4,5,3});[br][br][/br]list.print();[br][/br][br][/br]System.out.println(findBegining(list).data);[br][/br][br][/br]}[br][/br][br][/br]}[/code]编译没错误,运行有错,求解答,加大米[/i]
2条回复
热度排序

发表回复