338. Concatenate Two Linked Lists

EasyLinked ListLinked ListConcatenationPointer Manipulation

Given the heads of two singly linked lists list1 and list2, concatenate list2 to the end of list1 and return the head of the combined list.

Do not create new nodes — attach list2 directly to the tail of list1.

Input: Heads of two singly linked lists.

Output: Head of the concatenated linked list.

Examples

Example 1
Input: [1,2,3], [4,5,6]
Output: [1,2,3,4,5,6]
Explanation: Traverse list1 to tail(3). 3.next=head(4). Return list1 head(1).
Example 2
Input: [], [1,2,3]
Output: [1,2,3]
Explanation: list1 is empty. Return list2 directly.
Example 3
Input: [1,2,3], []
Output: [1,2,3]
Explanation: list2 is empty. Return list1 unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →