Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val
and next
. val
is the value of the current node, and next
is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev
to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement the MyLinkedList
class:
MyLinkedList()
Initializes theMyLinkedList
object.int get(int index)
Get the value of theindexth
node in the linked list. If the index is invalid, return-1
.void addAtHead(int val)
Add a node of the valueval
before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.void addAtTail(int val)
Append a node of valueval
as the last element of the linked list.void addAtIndex(int index, int val)
Add a node of valueval
before theindexth
a node in the linked list. If the index equals the length of the linked list, the node will be appended to the end of the linked list. If the index is greater than the length, the node will not be inserted.void deleteAtIndex(int index)
Delete theindexth
node in the linked list, if the index is valid.
Example:
Get Element -1
Add Element at head
Get Element 2
Add Element at tail
Get Element 3
Get Element -1
Approach
Java
public class MyLinkedList {public static void main(String[] args) {MyLinkedList obj = new MyLinkedList();System.out.println("Get Element " + obj.get(0));System.out.println("Add Element at head ");obj.addAtHead(2);System.out.println("Get Element " + obj.get(0));System.out.println("Add Element at tail ");obj.addAtTail(3);System.out.println("Get Element " + obj.get(1));obj.deleteAtIndex(1);System.out.println("Get Element " + obj.get(1));}private Node head;private Node tail;/** Initialize your data structure here. */public MyLinkedList() {head = new Node(-1);tail = new Node(-1);head.next = tail;tail.prev = head;}/*** Get the value of the index-th node in the linked list. If the index is* invalid, return -1.*/public int get(int index) {if (index < 0)return -1;int i = 0;Node p = head;for (; i < index && p.next != tail; i++) {p = p.next;}return p.next.val;}/*** Add a node of value val before the first element of the linked list. After* the insertion, the new node will be the first node of the linked list.*/public void addAtHead(int val) {Node n = new Node(val);n.next = head.next;n.prev = head;head.next = n;n.next.prev = n;}/** Append a node of value val to the last element of the linked list. */public void addAtTail(int val) {Node n = new Node(val);n.next = tail;n.prev = tail.prev;tail.prev = n;n.prev.next = n;}/*** Add a node of value val before the index-th node in the linked list. If index* equals to the length of linked list, the node will be appended to the end of* linked list. If index is greater than the length, the node will not be* inserted.*/public void addAtIndex(int index, int val) {int i = 0;Node p = head;for (; i < index && p.next != tail; i++) {p = p.next;}if (i != index)return;Node n = new Node(val);n.prev = p;n.next = p.next;p.next = n;n.next.prev = n;}/** Delete the index-th node in the linked list, if the index is valid. */public void deleteAtIndex(int index) {int i = 0;Node p = head;for (; i < index && p.next != tail; i++) {p = p.next;}if (p.next == tail)return;p.next = p.next.next;p.next.prev = p;}static class Node {public int val;public Node next;public Node prev;public Node(int val) {this.val = val;this.next = null;this.prev = null;}}}
C++
#include <bits/stdc++.h>using namespace std;vector<int> l;//Get the value of the index-th node in the linked list.//If the index is invalid, return -1.int get(int index){if (index < l.size())return l[index];elsereturn -1;}//Add a node of value val before the first element of//the linked list. After the insertion, the new node will// be the first node of the linked list.void addAtHead(int val){l.insert(l.begin(), val);}//Append a node of value val to the last element of the linked list.void addAtTail(int val){l.push_back(val);}//function to Add a node of value val before the index-th//node in the linked list. If index equals to the length of//linked list, the node will be appended to the end of linked list.// If index is greater than the length, the node will not be inserted.void addAtIndex(int index, int val){if (index < 0)addAtHead(val);else if (index <= l.size())l.insert(l.begin() + index, val);}//function to Delete the index-th node in the linked list,//if the index is validvoid deleteAtIndex(int index){if (index < l.size())l.erase(l.begin() + index);}int main(){cout << "Get Element " << get(0) << "\n";cout << "Add Element at head\n";addAtHead(2);cout << "Get Element " << get(0) << "\n";cout << "Add Element at tail\n";addAtTail(3);cout << "Get Element " << get(1) << "\n";deleteAtIndex(1);cout << "Get Element " << get(1) << "\n";}
No comments:
Post a Comment