Wrox Programmer Forums
|
BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition
This is the forum to discuss the Wrox book Programming Interviews Exposed: Secrets to Landing Your Next Job, 3rd Edition by John Mongan, Noah Kindler, Eric Giguere; ISBN: 978-1-118-26136-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 22nd, 2014, 05:34 PM
Registered User
 
Join Date: Dec 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Null or Cycle

I like my solution a little better than the book's:
Code:
// Javascript
function isCycle(list) {
  var slow,
      fast;

  slow = fast = list;

  while (fast.next) {
    slow = slow.next;
    fast = fast.next && fast.next.next;

    if (slow === fast || fast.next === slow) {
      return true;
    }
  }
  return false;
}
Fewer ifs and elses and sometimes while (true) can be a code smell.
 
Old December 22nd, 2014, 05:59 PM
Registered User
 
Join Date: Dec 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Entertainment Unit View Post
I like my solution a little better than the book's:
Code:
// Javascript
function isCycle(list) {
  var slow,
      fast;

  slow = fast = list;

  while (fast.next) {
    slow = slow.next;
    fast = fast.next && fast.next.next;

    if (slow === fast || fast.next === slow) {
      return true;
    }
  }
  return false;
}
Fewer ifs and elses and sometimes while (true) can be a code smell.
Ha, except I just realized it doesn't work for lists that have an even number of elements. Better:
Code:
function isCycle(list) {
  var slow,
      fast;

  slow = fast = list;

  while (fast.next) {
    slow = slow.next;
    fast = fast.next && fast.next.next;

    if (!fast) {
      return false;
    }

    if (slow === fast || fast.next === slow) {
      return true;
    }
  }
  return false;
}
The learning process illustrated!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cycle Through Files VB.NET 2003 Shades7_99 General .NET 1 February 26th, 2007 07:31 PM
asp.net page cycle mayurb Classic ASP Basics 1 January 2nd, 2007 01:13 PM
Tomcat life cycle listener dp_java Apache Tomcat 0 August 21st, 2006 06:39 AM
Tab cycle in Carbon projects geoforsy BOOK: Beginning Mac OS X Programming 0 October 19th, 2005 09:38 AM
Calculate Avg for cycle time pallavijyo BOOK: Professional Crystal Reports for VS.NET 0 December 10th, 2004 02:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.