Insert a Node at the Tail
EasyP3Coding
Insert a new node holding data at the tail (end) of a singly linked list and return the head. A list node has val and next. The ListNode class is already defined for you.
Example 1
- Input
- head = [1,2], data = 3
- Output
- [1,2,3]
Example 2
- Input
- head = [], data = 7
- Output
- [7]
Example 3
- Input
- head = [5], data = 5
- Output
- [5,5]
Constraints
- 0 <= number of nodes <= 1000
- -10^4 <= data, Node.val <= 10^4
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.