linked-list

java combine two linkedlist

99封情书 提交于 2020-08-25 07:10:05
问题 I have a question for combining two linkedlist. Basically, I want to append one linkedlist to the other linkedlist. Here is my solution. Is there a more efficient way to do it without looping the first linkedlist? Any suggestion would be appreciated. static Node connect(LinkedList list1, LinkedList list2) { Node original = list1.first; Node previous = null; Node current = list1.first; while (current != null) { previous = current; current = current.next; } previous.next = list2.first; return

java combine two linkedlist

自闭症网瘾萝莉.ら 提交于 2020-08-25 07:08:28
问题 I have a question for combining two linkedlist. Basically, I want to append one linkedlist to the other linkedlist. Here is my solution. Is there a more efficient way to do it without looping the first linkedlist? Any suggestion would be appreciated. static Node connect(LinkedList list1, LinkedList list2) { Node original = list1.first; Node previous = null; Node current = list1.first; while (current != null) { previous = current; current = current.next; } previous.next = list2.first; return

Time Complexity of Doubly Linked List Element Removal?

╄→гoц情女王★ 提交于 2020-08-21 09:17:52
问题 A lot of what I'm reading says that removing an internal element in a doubly linked list (DLL) is O(1) ; but why is this the case? I understand why it's O(n) for SLLs; traverse the list O(n) and remove O(1) but don't you still need to traverse the list in a DLL to find the element? 回答1: For a doubly linked list, it's constant time to remove an element once you know where it is. For a singly linked list, it's constant time to remove an element once you know where it and its predecessor are.

Move node to a new index in a linked list

岁酱吖の 提交于 2020-07-10 10:26:46
问题 i'm trying to change index in linked list.... something like: I have the linked list: 1->2->3->4->END and the user insert second node(2) and the index 4. The function will give us the result: 1->3->4->2->END. i know the algorithm is: find the node cut it out find the place to insert insert it but i cant make it work... my code of the function till now is: void changeIndex(FrameNode** head, char* name, int index) { FrameNode* temp = NULL; FrameNode* curr = *head; FrameNode* node = *head; int

Change index in a linked list in c

三世轮回 提交于 2020-07-10 07:52:26
问题 Can anyone help me with finding an algorithm to change a position of a node in a linked list according to the input? Example: I have the linked list: 1->2->3->4->END and the user chose the second node and the index 4. The algorithm will give us the result: 1->3->4->2->END. Thanks for your help! 回答1: It is really easy to move nodes in a linked list because they are done for that... To change a node's position, just change it's next pointer to the next node address, and the previous node's next

Change index in a linked list in c

爱⌒轻易说出口 提交于 2020-07-10 07:52:06
问题 Can anyone help me with finding an algorithm to change a position of a node in a linked list according to the input? Example: I have the linked list: 1->2->3->4->END and the user chose the second node and the index 4. The algorithm will give us the result: 1->3->4->2->END. Thanks for your help! 回答1: It is really easy to move nodes in a linked list because they are done for that... To change a node's position, just change it's next pointer to the next node address, and the previous node's next

Insertion at n'th place in Linked List showing Segmentation error

天涯浪子 提交于 2020-07-09 05:47:09
问题 void Insert(int data, int n){ Node* temp1 = new Node(); temp1 -> data = data; temp1 -> next = NULL; if(n == 1){ temp1 -> next = head; head = temp1; return; } else{ Node* temp2 = head; for(int i; i< n-2; i++){ temp2 = temp2 -> next; } temp1 -> next = temp2 -> next; temp2 -> next = temp1; } } I' m getting segmentation error on this and i m unable to figure out whats wrong. 回答1: void Insert(int data, int n) { Node* add = new Node(); // Node to be added add -> data = data; add -> next = NULL; if