-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedList.html
851 lines (798 loc) · 32.4 KB
/
linkedList.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="that js dude" content="">
<title>JS: Linked List Interview Questions</title>
<link rel="shortcut icon" href="images/favicon.jpg">
<link rel="stylesheet" href="css/bootstrap.min.css" >
<link rel="stylesheet" href="css/zenburn.css">
<!-- Custom styles for this template -->
<style>
/* Move down content because we have a fixed navbar that is 50px tall */
body {
padding-bottom: 20px;
}
.purpleBold{
color:purple;
font-weight: bold;
}
.gray{
color: gray;
}
.blueish{
color: rgba(151, 182, 209, 0.98);
}
.singInStuff{
margin-top: 9px;
}
#uName{
margin-top: -7px;
}
.skipListItem{
list-style-type: none;
}
.skipListItem a{
color: inherit;
}
a:visited
{
color: rgba(218, 209, 149, 0.98);
}
.padding10Px{
padding: 10px;
}
/*style for demo*/
</style>
<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
<div class="container">
<h1>JS: Interview Questions</h1>
<h2>part -4: Stack, Queue, Linked List</h2>
<p>June 7, 2014</p>
<div id="fb-root"></div><div class="fb-like" data-href="http://www.thatjsdude.com/interview/linkedList.html" data-layout="button_count" data-action="like" data-show-faces="false" data-share="false"></div><div class="g-plusone"></div>
</div>
</div>
<div class="container">
<!-- Example row of columns -->
<div class="row center">
<!-- <iframe width="853" height="480" src="//www.youtube.com/embed/Rx_JFOSxgpY" frameborder="0" allowfullscreen></iframe> -->
</div>
<!-- <p class="gray">if you are little more comfortable or claim to be comfortable with javascript, these questions would not be enough for you. more coming</p>
<p class="gray"> <span class="purpleBold">More Questions</span> <a href="css.html">css interview questions</a>, <a href="html.html">html interview questions</a> </p> -->
<div class="bg-info padding10Px">
<h2 class="text-danger">This page is Under Constructions</h2>
<h3>Read at your own risks</h3>
<h4 class="text-warning">Nothing Invented here!</h4>
<h4>Rather wheels in c/c++/java are reinvented in JavaScript.</h4>
<p>Just finding things in internet (ref provided) and solving them in JavaScript</p>
</div>
<div>
<h2>Basic Concept (beginners level)</h2>
<ol>
<li><a href="#stackAndQuue">Stack and Queue in JavaScript</a></li>
<li><a href="#priorityQueue">Priority Queue</a></li>
<li><a href="#singlyLinkedList">Singly Linked List</a></li>
<li><a href="#doublyLinkedList">Doubly Linked List</a></li>
<li><a href="#deleteNodeSLL">Remove a node from Singly Linked List</a></li>
<li><a href="#reverseSinglyLinkedList">Reverse a Singly Linked List</a></li>
<li><a href="#reverseDoublyLL">Reverse a Doubly Linked List</a></li>
<li><a href="#kthNodeFromEnd">Find Kth Node from End</a></li>
<li><a href="#deleteKthFromEnd">Delete Kth Node from End</a></li>
<li><a href="#deleteNodeDoublyLL">Delete a node from Doubly Linked List</a></li>
<li><a href="#detectLoop">Detect loop in a Singly Linked List</a></li>
<li><a href="#LoopStart">Detect the node where loop started</a></li>
<li class="skipListItem"><a href="#findMiddle">Find the middle node of a Singly LL</a></li>
<li><a href="#lengthSinglyLL">Find length of a Singly Linked List</a></li>
<li><a href="#loopWithDuplicate">Check duplicate value in a Singly with loop</a></li>
<li><a href="#rotateByKthNode">Rotate a Linked list by Kth Node</a></li>
<li><a href="#insertSortedLL">Insert node into a sorted LL</a></li>
<li><a href="#mergeUnsorted">Merge two unsorted Linked Lists</a></li>
<li><a href="#intersectOfTwoLL">Find intersect of two Linked List</a></li>
<li><a href="#sortLinkedList">sort Linked List</a></li>
<li><a href="#revmoveDuplicate">remove duplicate from unsorted LL</a></li>
<li><a href="#circularLinkedList">Create a Circular Linked List</a></li>
<li><a href="#checkPalindrome">Check Palindrome in Linked List</a></li>
<li><a href="#deletedQuestions">Deleted Questions</a></li>
</ol>
</div>
<div id="stackAndQuue">
<h2>Stack and Queue</h2>
<p><strong>Question:</strong> How will you implement stack and queue with JavaScript?</p>
<p><strong>Answer: </strong>stack is already implemented in JavaScritp array. you can simply call <code>push</code> and <code>pop</code> method.</p>
<pre><code>
var myStack = [];
//push
myStack.push(1);
myStack.push(2);
myStack.push(3);
//pop
myStack.pop(); //3
myStack.pop(); //2
myStack.pop(); //1
</code></pre>
<h4>Queue</h4>
<p>Queue is pretty much same. other than calling <code>pop</code> you will use <code>shift</code> method to get the element in the front side of your array.</p>
<pre><code>
var myQueue = [];
//push
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
//pop
myQueue.shift(); //1
myQueue.shift(); //2
myQueue.shift(); //3
</code></pre>
</div>
<div id="priorityQueue">
<h2>Priority Queue</h2>
<p>you have to use a maximizing heap to manage a priority queue. Hence while extracting, you just get the one on the top and while inserting you will do it at the bottom</p>
<pre><code>
//for now read the link.
</code></pre>
<p>ref: i have implemented simplified version of <a href="https://github.com/janogonzalez/priorityqueuejs/blob/master/index.js">priorityQueueJS</a></p>
</div>
<div id="singlyLinkedList">
<h2>Singly Linked List</h2>
<p><strong>Question: </strong> How will you create a linked list in JavaScript?</p>
<p><strong>Answer: </strong>if you don't have any idea about linked list. read: <a href="http://en.wikipedia.org/wiki/Linked_list">wiki: linked list</a></p>
<p>to have linked list, you need a node object that will point to the next. you need one pointers (head) to point the first node of your linked list. Besides, you need methods to add and remove node from your linked list</p>
<p>let's get started. We have an object LinkedList and that has one property head (a pointer)</p>
<pre><code>
function LinkedList(){
this.head = null;
}
</code></pre>
<p>to create add elements, will use a push method in the prototype of the object. push method will take value and will create a node object. if there is no head, then node will be the value of head. if there is a head. then we have to walk through the linked list chain to read the tail (tail is where the next node is null). We will simple set the of next at the tail to node.</p>
<pre><code>
LinkedList.prototype.push = function(val){
var node = {
value: val,
next: null
}
if(!this.head){
this.head = node;
}
else{
current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
}
}
</code></pre>
<p>Now. create a linked list and push values in it. </p>
<pre><code>
var sll = new LinkedList();
//push node
sll.push(2);
sll.push(3);
sll.push(4);
//check values by traversing LinkedList
sll.head; //Object {data: 2, next: Object}
sll.head.next; //Object {data: 3, next: Object}
sll.head.next.next; //Object {data: 4, next: null}
</code></pre>
<p>read: <a href="http://en.wikipedia.org/wiki/Linked_list">Advantage and disadvantage of linked list</a></p>
<p>copied from: <a href="http://www.nczonline.net/blog/2009/04/13/computer-science-in-javascript-linked-list/">Nicholas Zakas: linked list</a></p>
</div>
<div id="doublyLinkedList">
<h2>Doubly Linked List</h2>
<p>for doubly linked list there will be a link backward to the previous element. Your node object will look like</p>
<pre><code>
var node = {
value: val,
next: null,
previous: null
}
</code></pre>
<pre><code>
function DoublyLinkedList(){
this.head = null;
}
DoublyLinkedList.prototype.push = function(val){
var head = this.head,
current = head,
previous = head;
if(!head){
this.head = {value: val, previous:null, next:null };
}
else{
while(current && current.next){
previous = current;
current = current.next;
}
current.next = {value: val, previous:current, next:null}
}
}
</code></pre>
<pre><code>
//test at least once
var dll = new DoublyLinkedList();
dll.push(2);
dll.push(3);
dll.push(4);
dll.push(5);
//trust me it works
</code></pre>
<p>get most of it from: <a href="https://github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/doubly-linked-list/doubly-linked-list.js">doubly linked list</a></p>
</div>
<div id="deleteNodeSLL">
<h2>Remove Node from Singly LL</h2>
<p><strong>Question:</strong> How could you write an extension to remove a node from a LL</p>
<p><strong></strong></p>
<p>If you want to remove a node from your linked list you have to find the node. There are three conditions here</p>
<ul>
<li>case -1: your targeted node is in the head. you have to replace the head with the next node</li>
<li>case -2: your targeted node is in the tail. you just have to remove it from the tail. Hence next of the node before the tail will be null. </li>
<li>case-3: your targeted node is in the middle of the LinkedList, you have to make the node after your targeted node to be the next node of the node before your targeted node.</li>
</ul>
<p>let's do it</p>
<pre><code>
LinkedList.prototype.remove = function(val){
var current = this.head;
//case-1
if(current.value == val){
this.head = current.next;
}
else{
var previous = current;
while(current.next){
//case-3
if(current.value == val){
previous.next = current.next;
break;
}
previous = current;
current = current.next;
}
//case -2
if(current.value == val){
previous.next == null;
}
}
}
</code></pre>
<p>Now if you want to remove you can just pass the value</p>
<pre><code>
sll.remove(3);
sll.remove(1);
</code></pre>
<h5 class="text-danger bg-info padding10Px">remove last element doesnt work</h5>
</div>
<div id="reverseSinglyLinkedList">
<h2>Reverse Singly Linked List</h2>
<p><strong>Question: </strong> How would you reverse a singly LL (no loop)?</p>
<p><strong>Answer: </strong> Just walk through the LL and put the nodes in an array. This would be easier other than using remove function (if LL have one), because to remove a single item from the end of the SLL you have to walk all way to the end of the SLL every single time. Here you are just walking once.</p>
<h4>Iterative</h4>
<pre><code>
function reversesll(sll){
if(!sll.head || !sll.head.next) return sll;
var nodes=[],
current = sll.head;
//storing all the nodes in an array
while(current){
nodes.push(current);
current = current.next;
}
var reversedLL = new LinkedList();
reversedLL.head = nodes.pop();
current = reversedLL.head;
var node = nodes.pop();
//make sure to make next of the newly inserted node to be null
//other wise the last node of your SLL will retain its old next.
while(node){
node.next = null;
current.next = node;
current = current.next;
node = nodes.pop();
}
return reversedLL;
}
</code></pre>
<p>test at least once.</p>
<pre><code>
//create the LL
var sll = new LinkedList();
sll.push(1);
sll.push(2);
sll.push(3);
sll.push(4);
sll.push(5);
//test it
reversesll(sll);
//{head: {value:5, next:{value: 4, next: {value: 3, next: {value:2, next:{value:1, next: null}}}}}}
</code></pre>
<h4>Recursive</h4>
<pre><code>
//search it and implement it
</code></pre>
<p><strong>question</strong>Given number k, for Singly linked list, skip k nodes and then reverse k nodes, till the end.</p>
</div>
<div id="reverseDoublyLL">
<h2>Reverse Doubly LL</h2>
<p><strong>Question:</strong> reverse the doubly linked list without using extra space</p>
<p><strong>Answer:</strong></p>
<pre><code>
function reverseDoublyLL(dll){
var head = dll.head,
current = dll.head,
tmp;
while(current){
tmp = current.next;
current.next = current.previous;
current.previous = tmp;
if(!tmp){
//set the last node as header
dll.head = current;
}
current = tmp;
}
return dll;
}
</code></pre>
<pre><code>
//test it at least once
//or trust me it works
</code></pre>
<p>ref: <a href="http://www.careercup.com/question?id=5313007689138176">career cup</a></p>
</div>
<div id="kthNodeFromEnd">
<h2> kth node from End</h2>
<p><strong>Question: </strong> How could you get the Kth node from the end (no loop)</p>
<p><strong>Answer: </strong></p>
<pre><code>
function kthFromEnd(sll, k){
var node = sll.head,
i = 1,
kthNode;
//handle, 0 or negative value of k
if(k<=0) return;
while(node){
if(i == k) kthNode = sll.head;
else if(i-k>0){
kthNode = kthNode.next;
}
i++;
node = node.next;
}
return kthNode;
}
</code></pre>
<pre><code>
//create the LL
var sll = new LinkedList();
sll.push(1);
sll.push(2);
sll.push(3);
sll.push(4);
sll.push(5);
//test at least once
kthFromEnd(sll, 1); //Object {value: 5, next: null}
kthFromEnd(sll, 3); //Object {value: 3, next: Object}
</code></pre>
</div>
<div id="deleteKthFromEnd">
<h2>Delete kth node from the end</h2>
<p><strong>Question: </strong> How could you delete kth node from the end of a singly LL (no loop)</p>
<p><strong></strong></p>
<pre><code>
function deleteKthFromEnd(sll, k){
var node = sll.head,
i = 1,
kthNode,
previous;
if(k<=0) return sll;
while(node){
if(i == k) kthNode = sll.head;
else if(i-k>0){
previous = kthNode;
kthNode = kthNode.next;
}
i++;
node = node.next;
}
//kth node is the head
if(!previous)
sll.head = sll.head.next;
else{
previous.next = kthNode.next;
}
return sll;
}
</code></pre>
<p>test at least once</p>
<pre><code>
//test at least once
var sll = new LinkedList();
sll.push(1);
sll.push(2);
sll.push(3);
sll.push(4);
sll.push(5);
deleteKthFromEnd(sll, 2);
//{head: {value:1, next:{value: 2, next: {value: 3, next: {value:1, next:null}}}}}
</code></pre>
</div>
<div id="deleteNodeDoublyLL">
<h2>Delete a node from doubly LL</h2>
<p><strong>Question:</strong> How could you delete a node from a doubly linked list?</p>
<p><strong>Answer: </strong> there could be four cases</p>
<p><strong>delete head:</strong> very simple, make the second node as head. and dont forget to set previous of new head to null.</p>
<p><strong>node in the middle: </strong> skip break the chain, skip current node. hence next of previous node would be the next of the current node. Besides, previous of the next node would be previous node</p>
<p><strong>delete tail: </strong> its also simple, make the next of previous as null. </p>
<p><strong>didn't find:</strong> dont do anything</p>
<pre><code>
function deleteNodeDLL(val){
var current = dll.head,
previous;
//delete head
if(current.value == val){
dll.head = current.next;
//if there is only one node, then dll.head is null
if(dll.head) dll.head.previous = null;
return dll;
}
while(current.next){
if(current.value == val){
previous.next = current.next;
current.next.previous = previous;
return dll;
}
previous = current;
current = current.next;
}
//delete last node
if(current.value == val){
previous.next = null;
}
return dll;
}
</code></pre>
</div>
<div id="detectLoop">
<h2>Detect a loop in Singly Linked List</h2>
<pre><code>
function detectLoop(sll){
var slowPointer = sll.head,
fastPointer = sll.head;
while(slowPointer && fastPointer && fastPointer.next){
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
if(slowPointer == fastPointer){
return true;
}
}
return false;
}
</code></pre>
<pre><code>
//test at least once
var sll = new LinkedList();
sll.push(1);
sll.push(2);
sll.push(3);
sll.push(4);
sll.push(5);
detectLoop(sll); //false
//create a loop manually. point next of 5 to point 3
sll.head.next.next.next.next.next = sll.head.next.next
detectLoop(sll); //true
</code></pre>
</div>
<div id="LoopStart">
<h2>Detect Node where loop started</h2>
<p><strong>Question:</strong></p>
<p><strong>Answer:</strong> you will have two pointer slow and fast like you had to detect loop and they will meet somewhere. if fast one hit null (if thee is a way to hit null, fast one should hit if before the slower. because fast one moves fast (recally physics theories!)), there is no loop and returns null.</p>
<p>if there is a null and whenever both pointers meet, set slow pointer as head. Move both the pointer move by one step at a time</p>
<p>At the time of meet, fast one moved twice as much as slow one.</p>
<p><strong>think about it and find a way to explain it</strong></p>
<pre><code>
function findLoopStart(sll){
var slow = sll.head,
fast = sll.head;
while(slow && fast){
slow = slow.next;
//if hits null, then there is no loop
if(!fast.next){
return null;
}
fast = fast.next.next;
if(slow == fast){
slow = sll.head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
}
</code></pre>
<p>ref: <a href="http://umairsaeed.com/2011/06/23/finding-the-start-of-a-loop-in-a-circular-linked-list/">detect where loop started</a></p>
</div>
<div id="findMiddle">
<h2>Find middle</h2>
<p><strong>Question:</strong> How could you detect middle of a LL?</p>
<p><strong>Answer: </strong> need two pointer one will move two step another one. when two step will hit null. you will get middle for the singly. think about it.</p>
<pre><code>
//If you cant figure it out ourself. don't read further, do something else
</code></pre>
</div>
<div id="lengthSinglyLL">
<h2>Length of a Singly LL</h2>
<p><strong>Question:</strong> there could be loop in the singly LL</p>
<p><strong>Answer:</strong></p>
<pre><code>
//doesnt work.. somewhere gets stack overflow
//debug and find fix it
function getLength(sll){
var head = sll.head,
current = head,
pointer = head,
anotherPtr,
len = 0;
//use the previously written function
var loopStartNode = findLoopStart(sll);
if(!loopStartNode){
while(current){
current= current.next;
len++;
}
return len;
}
else{
anotherPtr = loopStartNode;
while(pointer != anotherPtr){
len += 2;
pointer = pointer.next;
anotherPtr = anotherPtr.next;
}
return len;
}
}
</code></pre>
</div>
<div id="loopWithDuplicate">
<h2>Loop with duplicate</h2>
<p><strong>Question:</strong> Write a function that will return true if a circular singly linked list has duplicate values</p>
<p><strong>Answer:</strong></p>
<pre><code>
//code it man! dont be lazy
</code></pre>
<p>Similar question: <a href="http://www.careercup.com/question?id=68277">Find the length of a linked list which contains cycle.</a></p>
<pre><code>
//just start a counter instead of checking duplicate
</code></pre>
<p>ref: <a href="http://www.careercup.com/question?id=15026777">career cup</a></p>
</div>
<div id="rotateByKthNode">
<h2>Rotate by Kth Node</h2>
<p><strong>Question:</strong> How could you rotate a Linked List by Kth Node?</p>
<p>or</p>
<p><strong>Question:</strong><a href="http://www.careercup.com/question?id=15434720">Append the last n nodes of a linked list to the beginning of the list </a></p>
<p><strong>Answer: </strong> if the given linked list is: 1->2->3->4->5 and k is 3,the list should be modified to: 4->5->1->2->3.</p>
<pre><code>
//will not work for k less than length-1
function rotateByKthNode(sll, k){
var prevHead = sll.head,
previous = sll.head,
current = sll.head,
i = 1;
while(current.next){
if(i==k+1){
sll.head = current;
previous.next = null;
}
previous = current;
current = current.next;
i++;
}
current.next = prevHead;
return sll;
}
</code></pre>
<pre><code>
rotateByKthNode(sll,3);
//{head: {value:4, next:{value: 5, next: {value: 1, next: {value:2, next:{value:3, next: null}}}}}}
</code></pre>
<p>ref: <a href="http://www.crazyforcode.com/rotate-linked-list-k-nodes/">rotate by kth Node</a></p>
</div>
<div id="insertSortedLL">
<h2>Insert Node to Sorted LL</h2>
<p><strong>Question:</strong> Insert node in a sorted LL</p>
<p><strong>Answer:</strong></p>
<pre><code>
function pushSorted(sll, val){
var head = sll.head,
current = head,
previous;
//value lower than head value
if(val < sll.head.value){
sll.head = {value: val, next: head};
return sll;
}
while(current){
if(current.value > val){
previous.next = {value: val, next: current};
return sll;
}
previous = current;
current = current.next;
}
//value higher than the last node value
previous.next = {value:val, next: null};
return sll;
}
</code></pre>
<pre><code>
var sll = new LinkedList();
sll.push(5);
sll.push(7);
sll.push(10);
sll.push(14);
pushSorted(sll, 9);
//{head: {value: 5, next: {value: 7, next: {value: 9, next: {value: 10, next: {value: 14, next: null}}}}}}
</code></pre>
</div>
<div id="mergeUnsorted">
<h2>merge two unsorted array</h2>
<p><strong>Question:</strong> How would you merge two unsorted linked list?</p>
<p><strong>Answer:</strong></p>
<p>ref: <a href="http://www.careercup.com/question?id=15421981">merge two unsorted linked list to one sorted LL</a></p>
<p>easier question: <a href="http://www.crazyforcode.com/merge-linked-list-linked-list-alternate-positions/">merge two LL in alternate position</a></p>
</div>
<div id="intersectOfTwoLL">
<h2>Intersect of two LL</h2>
<p><strong>Question:</strong> How could you find intersect of two SLL in single iteration?</p>
<p><strong>Answer:</strong></p>
<p>ref: <a href="http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/">most detail: have 6 methods</a> or <a href="http://twistedoakstudios.com/blog/Post3280_intersecting-linked-lists-faster">has some animation</a> or <a href="http://www.crazyforcode.com/write-function-intersection-point-linked-lists-y-shape-2/">intersection of two LL</a> or <a href="http://www.careercup.com/question?id=14952616">career cup</a></p>
</div>
<div id="sortLinkedList">
<h2>sort Linked list</h2>
<p><strong>Question:</strong> How would you sort a linked list?</p>
<p><strong>Answer:</strong> you can use bubble, merge or similar sort</p>
<pre><code>
</code></pre>
<p>ref: <a href="http://vijayinterviewquestions.blogspot.com/2007/05/sorting-linked-list.html">sort LL</a></p>
<h4>toggle as hard questions</h4>
<h2>special sort</h2>
<p><strong>Question:</strong> Given an integer linked list of which both first half and second half are sorted independently. Write a function to merge the two parts to create one single sorted linked list in place [do not use any extra space].</p>
<p><strong>Answer:</strong></p>
<p>ref: <a href="http://www.careercup.com/question?id=5724466092965888">career cup</a></p>
</div>
<div id="revmoveDuplicate">
<h2>Remove duplicate from unsorted LL</h2>
<p><strong>Question:</strong> Delete the repeated elements in a unsorted singly linked list in O(n) time complexity without using extra space.</p>
<p><strong>Answer:</strong></p>
<pre><code>
//get a crystal clear concept about space complexity
//also this question is in cracking the coding interview
</code></pre>
<p>ref: <a href="http://www.careercup.com/question?id=5740629665513472">career cup</a></p>
</div>
<div id="circularLinkedList">
<h2>circular Linked List</h2>
<p>If the next pointer of the last node points to the head node, it becomes circular linked list. On Singly or Doubly Linked List, you will know you hit the tail when you hit null. For circular linked list, you will know you hit the tail when you hit the head (the next of last one pointed to head).</p>
<pre><code>
function CircularLinkedList(){
this.head = null;
}
CircularLinkedList.prototype.push = function(val){
var head = this.head,
current = head,
node = {value: val, next: null, previous: null};
if(!head){
node.next = node;
node.previous = node;
this.head = node;
}
else{
while(current.next != head){
current = current.next;
}
node.next = head;
node.previous = current;
head.previous = node;
current.next = node;
}
}
</code></pre>
<pre><code>
var cll = new CircularLinkedList();
cll.push(3);
cll.push(4);
cll.push(5);
//trust me dude, it works
</code></pre>
<p>Now you can play with it to remove, sort, or insert into sorted or remove duplicate, blah blah blah</p>
<p>ref: <a href="http://www.martinbroadhurst.com/articles/circular-linked-list.html">circular linked list</a></p>
</div>
<div id="checkPalindrome">
<h2>Check whether a linked list is a palindrome</h2>
<p><strong>Question:</strong></p>
<p><strong>Answer:</strong></p>
<pre><code>
//get the solution from cracking the coding interview
//or ggl it
</code></pre>
</div>
<div id="deletedQuestions">
<h2>Deleted Questions</h2>
<ul>
<li>implement queue by using stack</li>
<li><a href="http://codercareer.blogspot.com/2011/10/no-17-queue-implemented-with-two-stacks.html">queue implement with two stack</a></li>
<li><a href="http://datastructureandalgoritms.blogspot.com/2012/01/interview-questions-3-stack-queue-list.html">implement circular queue</a></li>
<li><a href="http://www.crazyforcode.com/frequency-number-linked-list/">frequencey of given number in LL</a></li>
<li><a href="http://www.crazyforcode.com/delete-alternate-nodes-linked-list/">delete alternative nodes</a></li>
<li><a href="http://www.crazyforcode.com/move-node-front-linked-list/">move last node to the front</a></li>
<li><a href="http://www.youtube.com/playlist?list=PL3D11462114F778D7">youtube have stuff as well</a></li>
<li><a href="http://www.glassdoor.com/Interview/Given-a-binary-tree-convert-it-into-a-doubly-circular-linked-list-The-structure-of-the-tree-was-given-by-the-interviewer-QTN_290306.htm">Given a binary tree, convert it into a doubly circular linked list.</a> or <a href="http://www.careercup.com/question?id=15072737">ternary tree to LL</a></li>
<li><a href="http://www.careercup.com/question?id=24819662">Delete a node from SLL, in which the last node points to the middle node( in case of even no of nodes, it points to the first middle node) and update the SLL.</a></li>
<li><a href="http://www.careercup.com/question?id=14801886">how to sort a single linked list with out using an additional node?</a></li>
<li><a href="http://www.careercup.com/question?id=14911668">swap the consecutive elements of the list and return the head node. </a> or <a href="http://www.crazyforcode.com/swap-alternate-nodes-linked-list/">swap every two nodes </a></li>
<li><a href="http://www.careercup.com/question?id=11561969">Given a doubly linked list containing only three integers 1,2,3. Sort the list without exchanging the values.</a></li>
<li><a href="http://www.careercup.com/question?id=12267020">You are given two numbers in the form of linked list.Add them without reversing the linked lists</a></li>
<li><a href="http://www.careercup.com/question?id=12657669">Write a program to reverse every K elements of a linked list. </a></li>
<li><a href="http://www.careercup.com/question?id=11395723">Input: 1->2->3->4->a->b->c->d->5->6->e->f Output:1->a->2->b->3->c->4->d->5->e->6->f</a></li>
<li><a href="http://www.careercup.com/question?id=5988415816335360">Given a linked list with next and high pointers, populate high pointers to the next higher node, inplace and O(n).</a></li>
<li><a href="http://www.geeksforgeeks.org/sorted-insert-for-circular-linked-list/">Insert node to a sorted circular Linked List</a></li>
</ul>
</div>
<div>
<h3 class="purpleBold">Express anger!</h3>
<p class="gray">Feel free to express your anger (sorry folks, you have to use g+.). Also point out my mistakes ( technical, wrong answer, spelling, grammar, sentence..., whatever), let your dude learn and grow.</p>
<script src="https://apis.google.com/js/plusone.js"></script>
<div class="g-comments"
data-href="http://www.thatjsdude.com/interview/linkedList.html"
data-width="642"
data-first_party_property="BLOGGER"
data-view_type="FILTERED_POSTMOD">
</div>
</div>
<hr>
<footer>
<p>©thatJSDude 2014</p>
</footer>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery-2.0.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="js/toggleExample.js"></script>
<script type="text/javascript">
//social plugins
//g+
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
//fb
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>