查看: 1545| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 很简单的链表找环

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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

1.Node.java
  1. package Chapter2;

  2. public class Node<T> {
  3. public T data;
  4. public Node<T> next;
  5. public Node(T d, Node<T> n){
  6. data = d;
  7. next = n;
  8. }
  9. }
复制代码
2.MylinkedList.java
  1. package Chapter2;

  2. //Singly Linked List
  3. public class MyLinkedList<T> {
  4. public Node<T> head;

  5. public MyLinkedList(Node<T> h) {
  6. head = h;
  7. }
  8. public MyLinkedList() {
  9. head = null;
  10. }

  11. public MyLinkedList(T[] dataArray) {
  12. if (dataArray == null || dataArray.length <= 0)
  13. return;
  14. head = new Node<>(dataArray[0], null);
  15. Node<T> node = head;
  16. for (int i = 1; i < dataArray.length; i++) {
  17. node.next = new Node<T>(dataArray[i], null);
  18. node = node.next;
  19. }
  20. }

  21. public void print() {
  22. Node<T> cur = head;
  23. while (cur != null) {
  24. System.out.print(cur.data);
  25. if (cur.next != null) {
  26. System.out.print(" -> ");
  27. }
  28. cur = cur.next;
  29. }
  30. System.out.println();
  31. }

  32. public void prepend(T data) {
  33. Node<T> next = head != null ? head.next : null;
  34. head = new Node<T>(data, next);
  35. }

  36. public MyLinkedList<T> reverse() {
  37. if(head == null) return null;
  38. Node<T> newHead = head;
  39. Node<T> cur = head.next;
  40. head.next = null;
  41. while(cur != null) {
  42. Node<T> next = cur.next;
  43. cur.next = newHead;
  44. newHead = cur;
  45. cur = next;
  46. }
  47. head = newHead;
  48. return this;
  49. }
  50. }
复制代码
3.findbegning.java
  1. package Chapter2;

  2. public class findBeginning{

  3. public static<T> Node<T> findBegining(MyLinkedList<T> list){

  4. if(list.head==null||list==null)

  5. return null;

  6. Node<T> fast=list.head;
  7. Node<T> slow=list.head;

  8. while(fast!=null&&fast.next!=null){
  9. slow=slow.next;
  10. fast=fast.next.next;

  11. if(slow==fast) {break;
  12. }

  13. if(fast ==null|| fast.next==null){
  14. return null ;


  15. }
  16. }
  17. slow=list.head;
  18. while(slow!=fast){
  19. slow=slow.next;
  20. fast=fast.next;

  21. }

  22. return fast;
  23. }

  24. public static void main(String[] args){

  25. MyLinkedList<Integer> list=new MyLinkedList<>(new Integer[] {1,2,3,4,5,3});

  26. list.print();
  27. System.out.println(findBegining(list).data);


  28. }

  29. }
复制代码
编译没错误,运行有错,求解答,加大米



上一篇:two sorted lists fetch k largest sum
下一篇:Maximum Product Subarray
🔗
smzfeng 2014-9-26 08:18:04 | 只看该作者
全局:
MyLinkedList<Integer> list=new MyLinkedList<>(new Integer[] {1,2,3,4,5,3});

我认为这个链表没有环
回复

使用道具 举报

🔗
 楼主| TonyJang 2014-9-26 09:53:20 | 只看该作者
全局:
smzfeng 发表于 2014-9-26 08:18
MyLinkedList list=new MyLinkedList(new Integer[] {1,2,3,4,5,3});

我认为这个链表没有环

是的,我自己写testcase很少,没注意。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表