diff options
Diffstat (limited to 'middle_linked_list.c')
-rw-r--r-- | middle_linked_list.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/middle_linked_list.c b/middle_linked_list.c new file mode 100644 index 0000000..1284a23 --- /dev/null +++ b/middle_linked_list.c @@ -0,0 +1,23 @@ +// 876. Middle of the Linked List +#include "leetcode.h" + +/* + * given the head of a singly linked list, return the middle of the node of the + * linked list. if there are two middle nodes, return the second middle node. + */ + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode *middleNode(struct ListNode *head) { + struct ListNode *ans; + int c = 0, i; + for (ans = head; ans; ans = ans->next, c++) + ; + c /= 2; + for (i = 0, ans = head; i < c; ans = ans->next, i++) + ; + return ans; +} |