singly-linked-list

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

别等时光非礼了梦想. 提交于 2019-12-28 03:16:06
问题 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

Insert for singly-linked list C

徘徊边缘 提交于 2019-12-25 18:23:20
问题 I am having trouble with my insert to the front of the linked list fucntion in C #define arrSIZE = 100; struct listNode { char data[arrSIZE]; struct listNode *nextPtr; }; typedef struct listNode ListNode; void insertHead(ListNode *sPtr, char value[arrSIZE]){ ListNode *newPtr = (ListNode *)malloc(sizeof(ListNode)); strncpy(newPtr->data, value, arrSIZE); if(sPtr ==NULL){ newPtr->nextPtr=NULL; sPtr = newPtr; }else{ newPtr->nextPtr=sPtr; sPtr =newPtr; } } 回答1: I can see why. -You're setting sPtr,

Can we delete the last node of a Single Linked list if we only know the address of last node

陌路散爱 提交于 2019-12-25 07:48:47
问题 // Variables typedef struct node { int value; struct node *next; }mynode; // Globals (not required, though). mynode *head, *tail, *temp; // Functions void add(int value); // Function to add new nodes to the linked list void add(int value) { temp = (mynode *) malloc(sizeof(struct node)); temp->next=(mynode *)0; temp->value=value; if(head==(mynode *)0) { head=temp; tail=temp; } else { tail->next=temp; tail=temp; } } // The main() function int main() { head=(mynode *)0; // Construct the linked

Linked List not working for insertion

 ̄綄美尐妖づ 提交于 2019-12-25 02:34:18
问题 I have written a linked list code to insert a element in the node. But the problem is when i want to insert first element using function, the output is coming empty. But when i insert first element inside the main function (see comment line), it gives the correct output. How to solve it ? Here is my C code: #include<stdio.h> #include<stdlib.h> typedef struct node{ int val; struct node *next; }node; void print(node *head){ if(tem == NULL){ printf("List is Empty\n"); return; } node *tem= head;

How to find Intersections in two singly linked lists

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 01:46:39
问题 I am writing an program in which i have to set up data structure dictionary(singly linked list) with words alphabetically ordered(words that appear in sentence in a text document with document ids). and find which words appear in more than one document so the professor wants us to do an intersection. I am really confused on how to do the intersection. I have everything else(Which I believe is correct). Here is my code(I have added my intersect algorithm, but it is clearly not working and I

C - Nested linked lists

旧时模样 提交于 2019-12-24 19:05:08
问题 I'm trying to create a linked list of students, each with a linked list of grades, but I'm having trouble accessing the linked list of grades inside the linked list of students. typedef struct student_data_struct{ char student[MAX]; struct grades_list_struct *gradeP; } student_Data; typedef struct student_list_struct{ student_Data studentData; struct student_list_struct *next; } StudentNode; typedef struct grades_list_struct{ int grade; struct grades_list_struct *next; } GradeNode; GradeNode

Implement an algorithm to find the kth to last element of a singly linked list

岁酱吖の 提交于 2019-12-24 17:13:38
问题 Implement an algorithm to find the kth to last element of a singly linked list. Is a good solution to the above problem, reversing the linked list then traversing again and getting the kth element? 回答1: First of all the list is singly-linked . So that is a good hint that you should not try to reverse it, because you would need as much storage to make the copy. You can use a modified version of the turtle-hare algorithm: Use a hare pointer at the start of the list Move it at least K elements

Adding custom structure to GSList with Glib

北城余情 提交于 2019-12-24 05:46:08
问题 I'm trying to add a structure to a singly linked list with the function g_slist_append(list, &structure). This seems to work (it's adding the pointer), however I can't seem to find a way to view the elements in the structure when reading the linked list. My structure looks like this: struct customstruct { int var1; int var2; char *string_1; } Then, I make a list: GSList *list = NULL; Then, I append one instance of the structure like this: struct customstruct list_entry; list_entry.var1 = 1;

How do I keep a mutable reference to the last node while building a linked list?

若如初见. 提交于 2019-12-24 03:32:14
问题 I'm trying to implement building a singly-linked list from an Iterator , keeping the order of elements. The structure is defined as: #[derive(Debug)] struct List<T> { list: Node<T>, } type Node<T> = Option<Box<Link<T>>>; #[derive(Debug)] struct Link<T> { head: T, tail: Node<T>, } I was thinking of keeping a mutable reference to the end of the list and expanding it while iterating. However, I couldn't figure out how this could be done. The (non-working) idea was: impl<T> List<T> { pub fn from

printing nodes from a singly-linked list

可紊 提交于 2019-12-23 03:49:14
问题 I made a node class which is a linked list class. Is there any way I can print out elements in this list ? I made my print() method but it only returns the first element which is 21. How do I iterate through that list ? public class ListNode { private int item; private ListNode next; public ListNode(int item, ListNode next){ this.item = item; this.next = next; } public ListNode(int item){ this(item, null); } public int print(){ return item; } public static void main(String[] args) { ListNode