singly-linked-list

Interview Question: Merge two sorted singly linked lists without creating new nodes

这一生的挚爱 提交于 2019-11-27 09:59:13
This is a programming question asked during a written test for an interview. "You have two singly linked lists that are already sorted, you have to merge them and return a the head of the new list without creating any new extra nodes. The returned list should be sorted as well" The method signature is: Node MergeLists(Node list1, Node list2); Node class is below: class Node{ int data; Node next; } I tried many solutions but not creating an extra node screws things. Please help. Here is the accompanying blog entry http://techieme.in/merging-two-sorted-singly-linked-list/ Node MergeLists(Node

Swap nodes in a singly-linked list

只愿长相守 提交于 2019-11-27 08:46:34
I am trying to swap two nodes. For example if the nodes are a and b I am passing the pointers (a-1)->next and (b-1)->next which are basically nodes a and b . void swap(struct stack **a,struct stack **b) { struct stack *temp1 = *a, *temp2 = *b, *temp3 = *b; *a = *b; (*b)->next = (temp1)->next; temp2 = temp1; (temp2)->next = temp3->next; } What am I doing wrong? When I am trying to print the nodes after calling the function it's an infinite loop. Please help. Grijesh Chauhan Why Infinite loop? Infinite loop is because of self loop in your list after calling swap() function. In swap() code

Reversing a linkedlist recursively in c

折月煮酒 提交于 2019-11-27 05:35:28
问题 The following code works fine when head is sent as a parameter to it. As I am new to C, I couldn't understand how it works. Help me out please. struct node *recursiveReverseLL(struct node *list) { struct node *revHead; if (list == NULL || list->link == NULL) { return list; } revHead = recursiveReverseLL(list->link); list->link->link = list; list->link = NULL; return revHead; } I dont know how the links are provided using those recursive calls. ie) if the links are as, 1 -> 2 -> 3 -> 4 then hw

Java - Does returning a value break a loop?

筅森魡賤 提交于 2019-11-27 03:23:55
I'm writing some code that basically follows the following format: public static boolean isIncluded(E element) { Node<E> c = head; while (c != null) { if (cursor.getElement().equals(element)) { return true; } c = c.getNext(); } return false; } The code will search for an element in a list of nodes. However, my question is that if the while loop does find the element where the if-statement says it should return true, will it simply return true and break the loop? Furthermore, if it does then break the loop will it then carry on through the method and still return false, or is the method

Reverse Singly Linked List Java [duplicate]

微笑、不失礼 提交于 2019-11-27 03:01:38
This question already has an answer here: How to reverse a singly-linked list in blocks of some given size in O(n) time in place? 4 answers Can someone tell me why my code dosent work? I want to reverse a single linked list in java: This is the method (that doesnt work correctly) public void reverseList(){ Node before = null; Node tmp = head; Node next = tmp.next; while(tmp != null){ if(next == null) return; tmp.next = before; before = tmp; tmp = next; next = next.next; } } And this is the Node class: public class Node{ public int data; public Node next; public Node(int data, Node next){ this

Java - Does returning a value break a loop?

孤者浪人 提交于 2019-11-26 18:01:04
问题 I'm writing some code that basically follows the following format: public static boolean isIncluded(E element) { Node<E> c = head; while (c != null) { if (cursor.getElement().equals(element)) { return true; } c = c.getNext(); } return false; } The code will search for an element in a list of nodes. However, my question is that if the while loop does find the element where the if-statement says it should return true, will it simply return true and break the loop? Furthermore, if it does then

MIPS linked list

放肆的年华 提交于 2019-11-26 15:31:38
I'm confused exactly about how to create structures in MIPS. I want to create a linked list implementation which computes the length of the strings stored, and sorts them in stored-order. Here is my code so far: # Global symbols # # string routines .globl read_string .globl strcmp .globl strlen .globl trim .globl strloop .globl replace # list routines .globl insert .globl insert_here .globl print_list .globl main # pseudo-standard library .globl get_string .globl malloc .globl print_newline .globl print_string ################################################## # Constants # .data MAX_STR_LEN:

Swap nodes in a singly-linked list

↘锁芯ラ 提交于 2019-11-26 14:17:30
问题 I am trying to swap two nodes. For example if the nodes are a and b I am passing the pointers (a-1)->next and (b-1)->next which are basically nodes a and b . void swap(struct stack **a,struct stack **b) { struct stack *temp1 = *a, *temp2 = *b, *temp3 = *b; *a = *b; (*b)->next = (temp1)->next; temp2 = temp1; (temp2)->next = temp3->next; } What am I doing wrong? When I am trying to print the nodes after calling the function it's an infinite loop. Please help. 回答1: Why Infinite loop? Infinite

MIPS linked list

 ̄綄美尐妖づ 提交于 2019-11-26 04:27:48
问题 I\'m confused exactly about how to create structures in MIPS. I want to create a linked list implementation which computes the length of the strings stored, and sorts them in stored-order. Here is my code so far: # Global symbols # # string routines .globl read_string .globl strcmp .globl strlen .globl trim .globl strloop .globl replace # list routines .globl insert .globl insert_here .globl print_list .globl main # pseudo-standard library .globl get_string .globl malloc .globl print_newline

How to reverse a singly linked list using only two pointers?

拈花ヽ惹草 提交于 2019-11-26 00:30:58
问题 I wonder if there exists some logic to reverse a singly-linked list using only two pointers. The following is used to reverse the single linked list using three pointers namely p , q , r : struct node { int data; struct node *link; }; void reverse() { struct node *p = first, *q = NULL, *r; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } first = q; } Is there any other alternate to reverse the linked list? What would be the best logic to reverse a singly linked list, in terms of