본문 바로가기

Reverse Linked List Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up:A linked list can be reversed either iteratively or recursively. Could you implement both? My Solution:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverseList(self, head): """ :type head: ListNode :rtype: L.. 더보기
Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n will always be valid.Follow up:Could you do this in one pass? My solution:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x.. 더보기
Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list -- head = [4,5,1,9], which looks like following: 4 -> 5 -> 1 -> 9 Example 1:Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. Example 2:Input: h.. 더보기
Print the Elements of a Linked List If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.Input FormatThe void Print(Node* head) method takes the head node of a linked list as a parameter. Each struct Node has a data field (.. 더보기