注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
进入debug的时候有bug,实在找不到,求助大家
1.Node.java- package Chapter2;
-
- public class Node<T> {
- public T data;
- public Node<T> next;
- public Node(T d, Node<T> n){
- data = d;
- next = n;
- }
- }
复制代码 2.MylinkedList.java- package Chapter2;
-
- //Singly Linked List
- public class MyLinkedList<T> {
- public Node<T> head;
-
- public MyLinkedList(Node<T> h) {
- head = h;
- }
- public MyLinkedList() {
- head = null;
- }
-
- public MyLinkedList(T[] dataArray) {
- if (dataArray == null || dataArray.length <= 0)
- return;
- head = new Node<>(dataArray[0], null);
- Node<T> node = head;
- for (int i = 1; i < dataArray.length; i++) {
- node.next = new Node<T>(dataArray[i], null);
- node = node.next;
- }
- }
-
- public void print() {
- Node<T> cur = head;
- while (cur != null) {
- System.out.print(cur.data);
- if (cur.next != null) {
- System.out.print(" -> ");
- }
- cur = cur.next;
- }
- System.out.println();
- }
-
- public void prepend(T data) {
- Node<T> next = head != null ? head.next : null;
- head = new Node<T>(data, next);
- }
-
- public MyLinkedList<T> reverse() {
- if(head == null) return null;
- Node<T> newHead = head;
- Node<T> cur = head.next;
- head.next = null;
- while(cur != null) {
- Node<T> next = cur.next;
- cur.next = newHead;
- newHead = cur;
- cur = next;
- }
- head = newHead;
- return this;
- }
- }
复制代码 3.findbegning.java- package Chapter2;
- public class findBeginning{
- public static<T> Node<T> findBegining(MyLinkedList<T> list){
- if(list.head==null||list==null)
- return null;
- Node<T> fast=list.head;
- Node<T> slow=list.head;
- while(fast!=null&&fast.next!=null){
- slow=slow.next;
- fast=fast.next.next;
- if(slow==fast) {break;
- }
- if(fast ==null|| fast.next==null){
- return null ;
- }
- }
- slow=list.head;
- while(slow!=fast){
- slow=slow.next;
- fast=fast.next;
- }
- return fast;
- }
- public static void main(String[] args){
- MyLinkedList<Integer> list=new MyLinkedList<>(new Integer[] {1,2,3,4,5,3});
- list.print();
- System.out.println(findBegining(list).data);
- }
- }
复制代码 编译没错误,运行有错,求解答,加大米
|