singly-linked-list

Why is deleting in a single linked list O(1)?

那年仲夏 提交于 2019-11-30 03:15:05
I do not quiet understand why deleting at the end of a single linked list goes in O(1) time, as the wikipedia article says. A single linked list consists out of nodes. A node contains some kind of data, and a reference to the next node. The reference of the last node in the linked list is null. -------------- -------------- -------------- | data | ref | -> | data | ref | -> ... -> | data | ref | -------------- -------------- -------------- I indeed can remove the the last node in O(1). But in that case you don't set the reference of the newly last node, the previous one, to null since it still

Scheme - sum the squares of even-valued elements in a list

有些话、适合烂在心里 提交于 2019-11-29 16:41:50
I want to be able to sum the squares of the even elements in the list, however my current code only sums the elements, not the squares. Does anyone know of any modifications that can be made to make this to sum the squares of the even-valued elements in the list? (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) My input would be: (sum-evens (list 1 2 3 4)) Output would be: 20 Which is (2*2) + (4*4) . If possible, it would be good to see both a recursive and iterative solution. Any ideas? There are two possibilities, either we implement the recursion

Reversing a singly linked list iteratively

↘锁芯ラ 提交于 2019-11-29 16:30:33
问题 Has to be O(n) and in-place (space complexity of 1). The code below does work, but is there a simpler or better way? public void invert() { if (this.getHead() == null) return; if (this.getHead().getNext() == null) return; //this method should reverse the order of this linked list in O(n) time Node<E> prevNode = this.getHead().getNext(); Node<E> nextNode = this.getHead().getNext().getNext(); prevNode.setNext(this.getHead()); this.getHead().setNext(nextNode); nextNode = nextNode.getNext();

Time complexity of node deletion in singly- and doubly-linked lists

﹥>﹥吖頭↗ 提交于 2019-11-29 12:25:24
问题 Why is the time complexity of node deletion in doubly linked lists (O(1)) faster than node deletion in singly linked lists (O(n))? 回答1: The problem assumes that the node to be deleted is known and a pointer to that node is available. In order to delete a node and connect the previous and the next node together, you need to know their pointers. In a doubly-linked list, both pointers are available in the node that is to be deleted. The time complexity is constant in this case, i.e., O(1).

Why is deleting in a single linked list O(1)?

回眸只為那壹抹淺笑 提交于 2019-11-29 00:20:42
问题 I do not quiet understand why deleting at the end of a single linked list goes in O(1) time, as the wikipedia article says. A single linked list consists out of nodes. A node contains some kind of data, and a reference to the next node. The reference of the last node in the linked list is null. -------------- -------------- -------------- | data | ref | -> | data | ref | -> ... -> | data | ref | -------------- -------------- -------------- I indeed can remove the the last node in O(1). But in

How to read a singly linked list backwards?

非 Y 不嫁゛ 提交于 2019-11-28 18:27:06
One method which I can think of is to reverse the list and then read it. But this involves changing the list which is bad. OR I can make a copy of the list and then reverse it, but this uses additional O(n) memory. Is there any better method which doesn't use extra memory and doesn't modify the list and runs in O(n) time reverse linked list code is something like this in c# Void Reverse (Node head) { Node prev= null; Node current = head; Node nextNode = null; while (current!=null) { nextNode = current.Next; current.Next = prev; prev=current; current = nextNode; } head = prev; } Recursive

How to reverse a linked list?

白昼怎懂夜的黑 提交于 2019-11-28 18:22:22
Node reverse(Node head) { Node previous = null; Node current = head; Node forward; while (current != null) { forward = current.next; current.next = previous; previous = current; current = forward; } return previous; } How exactly is it reversing the list? I get that it first sets the second node to forward . Then it says current.next is equal to a null node previous . Then it says previous is now current . Lastly current becomes forward ? I can't seem to grasp this and how its reversing. Can someone please explain how this works? You reverse the list iteratively and always have the list in the

Reversing a linkedlist recursively in c

此生再无相见时 提交于 2019-11-28 05:31:42
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 is it changed as, 4 -> 3 -> 2 -> 1 codaddict The general recursive algorithm for this is: Divide the

How to read a singly linked list backwards?

空扰寡人 提交于 2019-11-27 11:21:26
问题 One method which I can think of is to reverse the list and then read it. But this involves changing the list which is bad. OR I can make a copy of the list and then reverse it, but this uses additional O(n) memory. Is there any better method which doesn't use extra memory and doesn't modify the list and runs in O(n) time reverse linked list code is something like this in c# Void Reverse (Node head) { Node prev= null; Node current = head; Node nextNode = null; while (current!=null) { nextNode

How to reverse a linked list?

你离开我真会死。 提交于 2019-11-27 11:17:07
问题 Node reverse(Node head) { Node previous = null; Node current = head; Node forward; while (current != null) { forward = current.next; current.next = previous; previous = current; current = forward; } return previous; } How exactly is it reversing the list? I get that it first sets the second node to forward . Then it says current.next is equal to a null node previous . Then it says previous is now current . Lastly current becomes forward ? I can't seem to grasp this and how its reversing. Can