Guest User

Untitled

a guest
Dec 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. FUNCTION length(LIST list) : INT
  2. // returns number of nodes until the end of the list
  3.  
  4. FUNCTION skip(LIST list, INT k) : LIST
  5. // return the sublist of list, skipping k nodes
  6.  
  7. FUNCTION intersection(LIST list1, list2) : LIST
  8. // returns the sublist where the two lists intersects
  9.   INT n1 = length(list1);
  10.   INT n2 = length(list2);
  11.  
  12.   IF (n1 > n2) THEN
  13.      list1 = skip(list1, n1-n2)
  14.   ELSE
  15.      list2 = skip(list2, n2-n1)
  16.  
  17.   WHILE (list1 != list2)
  18.     list1 = skip(list1, 1)
  19.     list2 = skip(list2, 1)
  20.  
  21.   RETURN list1
Add Comment
Please, Sign In to add comment