332. Doubly Linked List — Insert at End

EasyLinked ListDoubly Linked ListDLLInsertion

Given the head of a doubly linked list and an integer val, insert a new node with value val at the end. Update prev and next pointers. Return the head.

Traverse to the last node, attach new node, and update prev pointer.

Input: Head of a doubly linked list and integer val.

Output: Head of the updated doubly linked list.

Examples

Example 1
Input: [1,2,3], 4
Output: [1,2,3,4]
Explanation: Traverse to node(3). newNode(4).prev=3; 3.next=newNode(4). Return head(1).
Example 2
Input: [], 5
Output: [5]
Explanation: Empty list. New node is head.
Example 3
Input: [7], 8
Output: [7,8]
Explanation: One node. 7.next=8; 8.prev=7. Return 7.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →