Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 431f74e

Browse files
Sum of linked lists
1 parent edef607 commit 431f74e

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Has working code explain to understand fundamental and advance concepts with wor
1717
### Notes:
1818
Several diagrams, pds and other notes to help you understand better.
1919

20-
## Code is demoed and explained in videos on my :
20+
## Code is demoed and explained in videos on my Youtube channel:
2121
The play list for Algorithm and Data structure is
2222
https://www.youtube.com/watch?v=Qq8aW7EpKnE&list=PL13Vva6TJcSvS0sGHcy9dwhm1EcSGFRoZ
2323

‎src/crackingTheCodingInterview/chap2/2 QUESTIONS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ Given a circular linked list, implement an algorithm that returns the node at th
4141
A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
4242
EXAMPLE
4343
Input: A -> B -> C -> D -> E -> C[thesameCasearlier]
44-
Output: C
44+
Output: C
45+
46+
TODO:
47+
Intersection:
48+
Loop Detection
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package crackingTheCodingInterview.chap2;
2+
3+
import me.premaseem.myLib.MyDLLNode;
4+
import me.premaseem.myLib.MySLLNode;
5+
import org.junit.Test;
6+
7+
/**
8+
* ### Palindrome:
9+
* Implement a function to check if a linked list is a palindrome.
10+
*/
11+
public class PalindromeDoubleLinkedList {
12+
// Test Driven Development by Aseem Jain
13+
@Test
14+
public void test() {
15+
16+
// input
17+
int[] in = {1,2,3,3,2,1};
18+
MyDLLNode head = new MyDLLNode(in);
19+
assert solution1(head);
20+
21+
}
22+
23+
// Basic solution
24+
private boolean solution1(MyDLLNode head) {
25+
if (head == null) {
26+
return false; // depends on our definition of a palindrome.
27+
}
28+
MyDLLNode c = head;
29+
while(c.next != null){
30+
c = c.next;
31+
}
32+
MyDLLNode tail = c;
33+
MyDLLNode fast = head;
34+
35+
int count = 0;
36+
// find out tail and iterate it till half of the time
37+
while(head != null && tail != null && fast!= null && fast.next != null){
38+
if (head.data != tail.data){
39+
return false;
40+
}
41+
42+
head = head.next;
43+
tail = tail.prev;
44+
fast = fast.next.next;
45+
System.out.println(count++);
46+
}
47+
48+
49+
return true;
50+
}
51+
}

‎src/me/premaseem/myLib/MySLLNode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ public void print(){
4444
}
4545
System.out.println();
4646
}
47+
48+
public MySLLNode reverse(){
49+
MySLLNode c = this;
50+
MySLLNode p = null;
51+
while(c != null){
52+
MySLLNode n = c.next;
53+
c.next = p;
54+
p = c;
55+
c= n;
56+
}
57+
return p;
58+
}
4759
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /