Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 22nd, 2014, 04:34 PM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #2 (permalink)  
Old December 22nd, 2014, 04:59 PM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cycle Through Files VB.NET 2003 Shades7_99 General .NET 1 February 26th, 2007 06:31 PM
asp.net page cycle mayurb Classic ASP Basics 1 January 2nd, 2007 12: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 01:34 PM



All times are GMT -4. The time now is 08:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.